Skip to content

Entity modeling

In order to enable entity management to the EntityManager object, we need to mark them appropriately. We can do it done via configuration in xml file or via annotations. This section presents configurations made at the code level.

@Entity

In order to define a database entity, the class declaration should be marked with the annotation @Entity, e.g.

@Entity
public class Movie {
   // class field declaration
}

The @ Entity annotation also allows in the name attribute to define the name of the table in which Hibernate expects the entity value.

NOTE: Each entity must be tagged with @Entity.

@Table

The @Table annotation allows, like the@Entity annotation, to define a table name, but also to indicate the directory or name of the database in which it is located. It also allows you to define additional indexes and constraints (e.g. uniqueness of column or group values).

@Entity
@Table(name = "movies")
public class Movie {
   // table definition
}

NOTE: The @Table annotation is not required to correctly define the entity.

@Column

By default, field names are directly mapped to the column names in the database table. The @Column annotation allows you to specify a specific mapped name that will represent the database column name, e.g.:

@Entity
@Table(name = "movies")
public class Movie {

   private String name;

   @Column(name = "movie_title")
   private String title;

   // other class fields
}

This annotation also allows you to specify the length of the column, defines whether a value can be null, and whether uniqueness should be enforced.

NOTE: The database entity should define an argumentless constructor and the getters andsetters for all fields.

@SecondaryTable

This mechanism enables mapping database tables within one entity.

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "specialization")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Specialization {

  @Id
  private int id;

  private String type;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "teacher")
@SecondaryTable(name = "specialization")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {

  @Id
  private int id;

  @Embedded
  private Person person;

  @Column(table = "specialization")
  private String type;
}

In the example above, we declare that the Teacher entity consists of both theteacher table and the specialization table. The secondary table is declared with @SecondaryTable. The columns belonging to the second table must be assigned to the indicated table using the @Column(table = "specialization") statement.

@MappedSuperclass

Relational databases connect tables on the principle of bindings, while entities follow a fully object-oriented paradigm, therefore, many aspects of business logic can be solved through inheritance, which is not compatible with the relational database concept. The basic form of solving this problem is the use of the @MappedSuperclass annotation, which allows you to create base classes that will not be physically reflected in the tables, but will be used to define the base for an entity. All columns in the class marked with @MappedSuperclass will be included in the derived class, which is the actual implementation of the table.

@MappedSuperclass
public abstract class BaseEntity  {

    @Id
    private int id;
}
@Entity
public class ComputerGame extends BaseEntity {

    private String name;
    private String description;
    private String type;
}

Based on the above example, a ComputerGame table will be generated, in which, in addition to the name, description,type columns, there is a base class column: id.

@MappedSuperclass example

NOTE: @MappedSuperclass may but doesn't have to be used to define identifier in the base class.