What does mapping an Entity to database table mean
A Java class represents an Entity in ORM terminology. Mapping an Entity to a table means associating the java class(which represents the entity) to a database table where the fields of the entity correspond to the columns of database table.

Mapping a java class to a database table is done using @Table annotation.
Example, suppose we have a java class which represents an employee.
The class will look like :

public class Employee { 
     private String name;
     private int id;
     // getter and setter methods
}

Now there is a database table “employee” in which an employee record will be stored.

In order to map the above Employee class to the database table, we need to tell Hibernate the following :

  1. In which table, the record will be inserted when Employee object will be saved.
  2. Which columns of the database table will hold which fields of the class(mapping between database table columns and class fields).

For the above two purposes, Hibernate offers @Table annotation which defines the database table corresponding to the class and @Column annotation which defines the database table column corresponding to the class field.

@Table annotation is applied over the class and @Column annotation is applied over the fields or their getter methods.

What does mapping an Entity to two tables mean
The above scenario works perfect when all the fields of a class reside in the same database table.

But what if some fields of the class should be saved in some table and some fields should be saved in another table.
Current post aims at answering this question.

Solution
Suppose the above Employee class has some additional fields related to the employee such as his address, blood group, zip code etc., which should go in some other table, let’s say “employeedetail“.

To store these fields in some other table, we need to address following concerns:

  1. What will be the name of the other table : JPA provides an annotation @SecondaryTable.
    This is a class level annotation which allows you to specify a table which can store some fields of the annotated class.
  2. Which fields should go in this table :  There is an attribute named table of @Column annotation.
    This annotation is applied over a class field which allows you to specify the table in which the value of this field will be stored.

Summarizing the above two points, in order to store some fields of a class in another table :

  1. Add @SecondaryTable annotation with name attribute of this annotation having the name of secondary table.
  2. Add @Column annotation with table attribute having the name of table in which the value of this field should be saved.

Updated class definition will look as below. Note that the main database table name is employee and secondary table name is employeedetail.
The fields address, blood group, zip code will be saved in employeedetail.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.SecondaryTable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Table(name="employee")
@SecondaryTable(name="employeedetail")
public class Employee {
    
   @Id
   @GeneratedValue
   private int id;
     
   @Column(name="name")
   private String name;

   @Column(name="address", table="employeedetail")
   private String address;

   @Column(name="bloodgroup", table="employeedetail")
   private String bloodGroup;

   @Column(name="zipcode", table="employeedetail")
   private String zipCode;

   // getter and setter methods
}

Now when you create an object of Employee class and save it using save method of Hibernate’s session or persist method of JPA’s entity manager, its fields id and name are saved in employee table and fields address, bloodGroup and zipCode are saved in employeedetail table.

Let’s tweak in

  1. If table attribute is not provided with @Column annotation, the field value is stored in the main table by default.
  2. It table attribute is provided, it should match with the value given in @SecondaryTable column.
  3. It is also possible to map fields of one class to multiple tables at the same time using @SecondaryTables annotation.
    The list of tables is provided using multiple @SecondaryTable annotations.

Leave a Reply