To overcome this problem we will use annotation @JoinTable.
@JoinTable:
Target:
Fields (including property get methods)
Used in the mapping of associations. It is specified on the owning side of an association.
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).
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.
If the JoinTable 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.
Target:
Fields (including property get methods)Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.
@JoinColumns:
Target:
Fields (including property get methods)Defines mapping for composite foreign keys. This annotation groups JoinColumn annotations for the same relationship. When the JoinColumns annotation is used, both the name and the referencedColumnName elements must be specified in each such JoinColumn annotation.
Look the following snap of the output..
Now we look for how to adding key to the collection table.
UserDetails.java
package com.sdnext.hibernate.tutorial.dto;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
@Entity
@Table (name=”USER_DETAIL”)
public class UserDetails
{
@Id
@Column(name=”USER_ID”)
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column(name=”USER_NAME”)
private String userName;
@ElementCollection
@JoinTable(name=”USER_ADDRESS”,
joinColumns=@JoinColumn(name=”USER_ID”))
@GenericGenerator(strategy=”hilo”, name = “hilo-gen”)
@CollectionId(columns = { @Column(name=”ADDRESS_ID”) }, generator = “hilo-gen”, type = @Type(type=”long”))
private Collection<Address> lisOfAddresses = new ArrayList<Address>();
public Collection<Address> getLisOfAddresses() {
return lisOfAddresses;
}
public void setLisOfAddresses(Collection<Address> lisOfAddresses) {
this.lisOfAddresses = lisOfAddresses;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String toString()
{
return “[User Name: “+userName+”n Office Address: “+lisOfAddresses+”]”;
}
}
Address.java and hibernate.cfg.xml is the same as previous chapter.
HibernateTestDemo.java
package com.sdnext.hibernate.tutorial;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.sdnext.hibernate.tutorial.dto.Address;
import com.sdnext.hibernate.tutorial.dto.UserDetails;
public class HibernateTestDemo {
/**
* @param args
*/
public static void main(String[] args)
{
UserDetails user = new UserDetails();
//user.setUserId(1);
user.setUserName(“Dinesh Rajput”);
Address address1 = new Address();
address1.setStreet(“First Street”);
address1.setCity(“First City”);
address1.setState(“First State”);
address1.setPincode(“First Pin”);
Address address2 = new Address();
address2.setStreet(“Second Street”);
address2.setCity(“Second City”);
address2.setState(“Second State”);
address2.setPincode(“Second Pin”);
user.getLisOfAddresses().add(address1);
user.getLisOfAddresses().add(address2);
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
}
}
Tables:
In Next Chapter we will see about that Proxy Object and Fetching strategies.
<<Previous Chapter 14<< >>Next Chapter16
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.