<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="color: #20124d;">In previous tutorial of Collections in Hibernate and Adding Keys Example we wrote the collection of objects of an entity class, we persist in the separate table, here there are following two tables are created in the database.</div>
<div style="color: #20124d;"></div>
<div style="color: #20124d;">1. <b>TBL_USER_DETAILS</b> for <b>UserDetails </b>entity type object</div>
<div style="color: #20124d;">2. <b>TBL_USER_DETAILS_lisOfAddresses </b>for <b>Address </b>value type object</div>
<div style="color: #20124d;"></div>
<div style="color: #20124d;">In this chapter we will explore some advanced options about the collection.</div>
<div style="color: #20124d;">Now we see first thing is the <b>name of the table</b>.</div>
<div style="color: #20124d;"></div>
<div style="color: #20124d;"><b>TBL_USER_DETAILS_lisOfAddresses </b>is name for the collection table it is not very user friendly name.</div>
<p>To overcome this problem we will use annotation <b style="color: orange;">@JoinTable.</b><br />
<b style="color: orange;"> @JoinTable:</b><br />
<b style="color: orange;"> </b><b>Target:</b><br />
Fields (including property get methods)</p>
<p>Used in the mapping of associations. It is specified on the owning side of an association.<br />
A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations. It may also be used to map bidirectional many-to-one/one-to-many associations, unidirectional many-to-one relationships, and one-to-one associations (both bidirectional and unidirectional).</p>
<p>When a join table is used in mapping a relationship with an embeddable class on the owning side of the relationship, the containing entity rather than the embeddable class is considered the owner of the relationship.</p>
<p>If the <b style="color: orange;">JoinTable </b>annotation is missing, the default values of the annotation elements apply. The name of the join table is assumed to be the table names of the associated primary tables concatenated together (owning side first) using an underscore.</p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b style="color: #274e13;">@Entity<br />
@Table (name=&#8221;USERS_DETAILS&#8221;)</b><br />
public class UserDetails<br />
{<br />
<b style="color: #274e13;"> @Id<br />
@Column(name=&#8221;USER_ID&#8221;)<br />
@GeneratedValue(strategy=GenerationType.AUTO)</b><br />
private int userId;<br />
<b style="color: #274e13;"><br />
@Column(name=&#8221;USER_NAME&#8221;) </b><br />
private String userName;<b style="color: #274e13;">@ElementCollection<br />
@JoinTable(name=&#8221;USER_ADDRESS&#8221;)</b>//name of the table is changed to USER_ADDRESS<br />
private Collection<;Address>; lisOfAddresses = new ArrayList<;Address>;();<br />
}</p>
</div>
</div>
<p> ;</p>
<div id="ads-id" align="center"></div>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2016/12/address.jpg" width="640" height="70" border="0" /></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: left;">After running the above code the following output string show the updated table name of the collection table.</div>
<div class="separator" style="clear: both; text-align: left;"></div>
<div class="separator" style="clear: both; text-align: left;">Now we see that the name of the column of this table which have the foreign key user id is the <b>USER_DETAILS_USER_ID </b>is not also very user friendly, for this look to the following updated code of the <b>UserDetails</b> entity class. Here we used annotation <b>@JoinColumn</b>.</div>
<div class="separator" style="clear: both; text-align: left;"><b>@JoinColumn</b>:</div>
<p><b>Target:</b><br />
Fields (including property get methods)Specifies a column for joining an entity association or element collection. If the <b>JoinColumn </b>annotation itself is defaulted, a single join column is assumed and the default values apply.</p>
<p><b style="color: orange;">@JoinColumns</b>:<br />
<b>Target:</b><br />
Fields (including property get methods)Defines mapping for composite foreign keys. This annotation groups <b>JoinColumn </b>annotations for the same relationship. When the <b>JoinColumns </b>annotation is used, both the name and the referencedColumnName elements must be specified in each such JoinColumn annotation.</p>
<div class="separator" style="clear: both; text-align: left;"></div>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b style="color: #274e13;">@Entity<br />
@Table (name=&#8221;USERS_DETAILS&#8221;)</b><br />
public class UserDetails<br />
{<br />
<b style="color: #274e13;"> @Id<br />
@Column(name=&#8221;USER_ID&#8221;)<br />
@GeneratedValue(strategy=GenerationType.AUTO)</b><br />
private int userId;<br />
<b style="color: #274e13;"><br />
@Column(name=&#8221;USER_NAME&#8221;) </b><br />
private String userName;<b style="color: #274e13;">@ElementCollection<br />
@JoinTable(name=&#8221;USER_ADDRESS&#8221;,<br />
joinColumns=@JoinColumn(name=&#8221;USER_ID&#8221;))</b><br />
private Collection<;Address>; lisOfAddresses = new ArrayList<;Address>;();<br />
}</p>
</div>
<p>Look the following snap of the output..</p>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2016/12/address1.jpg" width="640" height="76" border="0" /></div>
<p>Now we look for how to adding key to the collection table.</p>
<p><b>UserDetails.java</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p>package com.sdnext.hibernate.tutorial.dto;</p>
<p>import java.util.ArrayList;<br />
import java.util.Collection;</p>
<p>import javax.persistence.Column;<br />
import javax.persistence.ElementCollection;<br />
import javax.persistence.Entity;<br />
import javax.persistence.GeneratedValue;<br />
import javax.persistence.GenerationType;<br />
import javax.persistence.Id;<br />
import javax.persistence.JoinColumn;<br />
import javax.persistence.JoinTable;<br />
import javax.persistence.Table;</p>
<p>import org.hibernate.annotations.CollectionId;<br />
import org.hibernate.annotations.GenericGenerator;<br />
import org.hibernate.annotations.Type;</p>
<p><b style="color: #274e13;">@Entity<br />
@Table (name=&#8221;USER_DETAIL&#8221;)</b><br />
public class UserDetails<br />
{<br />
<b>@Id<br style="color: #274e13;" /> @Column(name=&#8221;USER_ID&#8221;)<br style="color: #274e13;" /> @GeneratedValue(strategy=GenerationType.AUTO)</b><br />
private int userId;</p>
<p><b style="color: #274e13;">@Column(name=&#8221;USER_NAME&#8221;) </b><br />
private String userName;</p>
<p><b> @ElementCollection</b><br />
<b style="color: blue;">@JoinTable(name=&#8221;USER_ADDRESS&#8221;,<br />
joinColumns=@JoinColumn(name=&#8221;USER_ID&#8221;))<br />
@GenericGenerator(strategy=&#8221;hilo&#8221;, name = &#8220;hilo-gen&#8221;)<br />
@CollectionId(columns = { @Column(name=&#8221;ADDRESS_ID&#8221;) }, generator = &#8220;hilo-gen&#8221;, type = @Type(type=&#8221;long&#8221;))</b><br />
private Collection<;Address>; lisOfAddresses = new ArrayList<;Address>;();</p>
<p>public Collection<;Address>; getLisOfAddresses() {<br />
return lisOfAddresses;<br />
}<br />
public void setLisOfAddresses(Collection<;Address>; lisOfAddresses) {<br />
this.lisOfAddresses = lisOfAddresses;<br />
}<br />
public int getUserId() {<br />
return userId;<br />
}<br />
public void setUserId(int userId) {<br />
this.userId = userId;<br />
}<br />
public String getUserName() {<br />
return userName;<br />
}<br />
public void setUserName(String userName) {<br />
this.userName = userName;<br />
}<br />
public String toString()<br />
{<br />
return &#8220;[User Name: &#8220;+userName+&#8221;n Office Address: &#8220;+lisOfAddresses+&#8221;]&#8221;;<br />
}<br />
}</p>
</div>
<p><b>Address.java </b>and<b> hibernate.cfg.xml </b>is the same as previous chapter<b>.</b><br />
<b><br />
</b><br />
<b>HibernateTestDemo.java</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p>package com.sdnext.hibernate.tutorial;</p>
<p>import org.hibernate.Session;<br />
import org.hibernate.SessionFactory;<br />
import org.hibernate.cfg.AnnotationConfiguration;</p>
<p>import com.sdnext.hibernate.tutorial.dto.Address;<br />
import com.sdnext.hibernate.tutorial.dto.UserDetails;</p>
<p>public class HibernateTestDemo {<br />
/**<br />
* @param args<br />
*/<br />
public static void main(String[] args)<br />
{<br />
UserDetails user = new UserDetails();<br />
//user.setUserId(1);<br />
user.setUserName(&#8220;Dinesh Rajput&#8221;);</p>
<p>Address address1 = new Address();<br />
address1.setStreet(&#8220;First Street&#8221;);<br />
address1.setCity(&#8220;First City&#8221;);<br />
address1.setState(&#8220;First State&#8221;);<br />
address1.setPincode(&#8220;First Pin&#8221;);</p>
<p>Address address2 = new Address();<br />
address2.setStreet(&#8220;Second Street&#8221;);<br />
address2.setCity(&#8220;Second City&#8221;);<br />
address2.setState(&#8220;Second State&#8221;);<br />
address2.setPincode(&#8220;Second Pin&#8221;);</p>
<p>user.getLisOfAddresses().add(address1);<br />
user.getLisOfAddresses().add(address2);</p>
<p>SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();<br />
Session session = sessionFactory.openSession();<br />
session.beginTransaction();<br />
session.save(user);<br />
session.getTransaction().commit();<br />
session.close();<br />
}<br />
}</p>
</div>
<div style="background-color: #ff99cc; border: medium solid;"><b>Output:</b></div>
<div style="background-color: #ff99cc; border: medium solid;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: insert into USER_DETAIL (USER_NAME) values (?)<br />
Hibernate: insert into USER_ADDRESS (USER_ID, ADDRESS_ID, CITY_NAME, PIN_CODE, STATE_NAME, STREET_NAME) values (?, ?, ?, ?, ?, ?)<br />
Hibernate: insert into USER_ADDRESS (USER_ID, ADDRESS_ID, CITY_NAME, PIN_CODE, STATE_NAME, STREET_NAME) values (?, ?, ?, ?, ?, ?)</div>
</div>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2016/12/address2.jpg" width="640" height="83" border="0" /></div>
<p><b>Tables</b>:</p>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2016/12/table1.jpg" border="0" /></div>
<p>In <a href="https://dineshonjava.com/p/proxy-objects-and-eager-and-lazy-fetch.html">Next Chapter</a> we will see about that <a href="https://dineshonjava.com/p/proxy-objects-and-eager-and-lazy-fetch.html">Proxy Object and Fetching strategies</a>.</p>
<p><b> <;<;<a href="https://dineshonjava.com/p/saving-collections.html">Previous Chapter 14</a><;<; >;>;N<a href="https://dineshonjava.com/p/proxy-objects-and-eager-and-lazy-fetch.html">ext Chapter16</a></b></p>
</div>

View Comments
must see..nice
Good
Hi Dinesh, Now address has primary key so will it be entity class but not value type even we still need to use @Embeddable
Yes right.