Pages Navigation Menu

Coding is much easier than you think

Configuring & Adding Primary Key to Collections in Hibernate


 
In previous article we learnt on how to persist a collection of objects of an entity class in a separate table using hibernate. And below are the following two tables are created in the database.
 

 
1. STUDENT for Student entity type object
2. Student_lisOfAddress for Address value type object (The default table name for this table gets generated based on – Entity class name_Name of the embeddable object in the entity class).
Here ‘Student_ID’ column in Student_lisOfAddress is the foreign key reference of Student tables ‘ID’. In next article we shall learn to override the default collection class table name and to add primary key to it.
 
In this post we shall learn on how to change this default table name and foreign key name of collection table and how to add primary key to this table to identify each record separately.
 

Changing table name foreign key column name of collection table

 
To do this we need to add @JoinTable annotation before the collection in Entity class as show below.
 

@ElementCollection
@JoinTable(name="ADDRESS",joinColumns=@JoinColumn(name="STUDENT_ID"))
private Collection
lisOfAddress = new ArrayList
();

 
In parameter of @JoinTable table we specify name of table and name of joinColumns (i.e foreign key) with another annotation @JoinColumn. When you run the test program with the above changes, the following quires are displayed in eclipse console which have our overridden table name primary key name.
 

insert into STUDENT(COLLEGE, DEPARTMENT, NAME, ID) values (?, ?, ?, ?)
insert into ADDRESS(STUDENT_ID,CITY_NAME,PIN_CODE,STATE_NAME,STREET_NAME) values (?,?,?,?,?)
insert into ADDRESS(STUDENT_ID,CITY_NAME,PIN_CODE,STATE_NAME,STREET_NAME) values (?,?,?,?,?)

 

Inserting Primary key for collection inside entity class

 
To inset the primary key collection table I have modified the collection element in Entity class as shown below.
 

@ElementCollection
@JoinTable(name="ADDRESS",joinColumns=@JoinColumn(name="STUDENT_ID"))
@GenericGenerator(name="myGenerator", strategy="increment")
@CollectionId(columns = {@Column(name="ADDRESS_ID")},
                          generator="myGenerator",type=@Type(type="int"))
private Collection
lisOfAddress = new ArrayList
();

 

Here are the topics of interest for our code above:
  • Since the primary key is to be created at collection table, we use the @CollectionId instead of @Id annotation before the collection in Entity class.
  • With the @GenericGenerator annotation, we will to give some information to @CollectionId and tell how it should work. First off, we name this GenericGenerator whatever we like, for my example I just named it “myGenerator” but this is completely up to you. We also tell it what ID generation strategy to use, in this case I used “INCREMENT” strategy.
  • Oh, and let’s not forget to point our @GeneratedValue annotation to our newly minted customer @ CollectionId via the generator=”myGenerator” property.

 
Address.java and hibernate.cfg.xml and HibernateUtil.java is the same as previous post.
 

Run it – Eclipse console

 
On running the Hibernate test class with above changes, the collection table gets created as below.
 

 
Here ADDRESS_ID is the primary key which gets generated due to above code change.
 
Voila!
 
download