JPA SecondaryTable Annotation Example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Consider a situation in which you have a legacy database in which the information about an entity is stored in multiple tables and you want JPA entity to store all the information in single entity. i.e. you want to map a single entity to multiple tables in database such that some fields from entity are stored in one table and other fields into other table. This article will elaborate JPA SecondaryTable Annotation Example

JPA SecondaryTable Annotation Example:

We are going to use secondary table to fulfil our requirement in JPA SecondaryTable annotation example. Here is what official oracle javadocs say about @SecondaryTable

Specifies a secondary table for the annotated entity class. Specifying one or more secondary tables indicates that the data for the entity class is stored across multiple tables.

 

Consider we have two tables with in database as follows.

  • EMPLOYEE table has columns IDEMPLOYEE, FIRST_NAME and LAST_NAME
  • EMPLOYEEDETAILS table has columns EMPLOYEEID, EMAILID, JOININGDATE and SALARY 

Database diagram is as follows

JPA Secondary Table Annotation Example

JPA Secondary Table Annotation Example

ID_GEN table is used for automatic id creation using JPA table generator strategy.

Now we have to create the Employee entity for JPA SecondaryTable annotation example. It is as below.

Important things to notice in JPA SecondaryTable annotation example are as below:

  • @SecondaryTable  specified on Employee entity denotes that some fields from Employee entity will be stored in a separate table.
  • name = "EMPLOYEE_DETAILS"  element specifies the name of SecondaryTable i.e. EMPLOYEE_DETAILS is used to store the information about employees in EMPLOYEE table. These tables will be connected by foreign key.
  • pkJoinColumns = @PrimaryKeyJoinColumn(name = "EMPLOYEE_ID")  element specifies the name of primary key column EMPLOYEE_ID in secondary table  EMPLOYEE_DETAILS which acts as foreign key column to join to primary table i.e. EMPLOYEE. 
  • @Column(name = "EMAILID", table = "EMPLOYEE_DETAILS")  maps email field of Employee entity to EMAILID column of EMPLOYEE_DETAILS
  • @Column(name = "FIRST_NAME")  . Here, table is not specified hence it will be mapped to FIRST_NAME column in EMPLOYEE table

Entity and database mappings are done for this JPA SecondaryTable annotation example. Let us configure persistence.xml

Let us create some employees and test it out.

After running the program, you can see entires in database tables created as follows

EMPLOYEE table

IDEMPLOYEE FIRST_NAME LAST_NAME
5 Prasad Kharkar
6 Sushant Pangarkar

EMPLOYEE_DETAILS table

EMPLOYEE_ID EMAILID JOININGDATE SALARY
5 prasad_kharkar@abc.com 0
6 800000

I hope this JPA SecondaryTable annotation example helped understand when and how secondary tables are used. Happy learning 🙂

Share Button

1 comment for “JPA SecondaryTable Annotation Example

  1. Raman B.
    December 24, 2015 at 12:45 pm

    In case when you’re trying to update Employee entity and there is a row in table EMPLOYEE_DETAILS which has only employee_id, then it is unable to save, instead of update hibernate run’s insert on EMPLOYEE_DETAILS table.

Leave a Reply

Your email address will not be published. Required fields are marked *