In this example you will learn how to deal with persisting entities that have members that are collections like lists, and maps. We will use the @ElementCollection annotation.
to define the collection of Embeddable objects. As a disclaimer, this is not a typical usage of Embeddable objects as the objects are not embedded in the source object’s table, but stored in a separate collection table. This is similar to a OneToMany, except the target object is an Embeddable instead of an Entity. This allows collections of simple objects to be easily defined, without requiring the simple objects to define an Id or ManyToOne inverse mapping.
For example let’s create a model of Distributors who sell Principal-Protected Notes on an open market, after the help of having a Guarantor and a Manufacturer who helps
and designs, and guarantees the principal and return at maturity of the PPNs. We will establish this as a One to Many relationship,
which each Distributor entity will contain the list of PPN’s that the can get. (These PPN’s entities in the financial market represent debt-like instruments with maturity dates that
are not protected by CDIC even though they are issued by chartered banks that can be Index-linked, mutual fund-linked, or Hedge fund-linked.)
The Distributor class shows how to use the @ElementCollection with @JoinTable
to use PrincipalProtectedNote as a collection mapped to a table.
package org.anthonykuong.dto;
import java.util.HashSet;
import java.util.Set;
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;
@Entity
public class Distributor implements java.io.Serializable {
private static final long serialVersionUID = -265840783731252465L;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
@ElementCollection
@JoinTable(name="DISTRIBUTOR_PPN_MAP",
joinColumns=@JoinColumn(name="distributor_id")
)
private Set listofPPNs = new HashSet();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setListofPPNs(Set listofPPNs) {
this.listofPPNs = listofPPNs;
}
public Set getListofPPNs() {
return listofPPNs;
}
}
Here is the Embeddable Principal Protected Note
package org.anthonykuong.dto;
import javax.persistence.Embeddable;
@Embeddable
public class PrincipalProtectedNote {
private int ppnId;
private String ppnName;
private boolean isZeroCouponBonds;
private boolean isCPPIStructured;
private String ppnDescription; //contains information such as the type of ppn,
//whether it is index-linked, mutual fund-linked, hedge fund-linked etc.
public int getPpnId() {
return ppnId;
}
public void setPpnId(int ppnId) {
this.ppnId = ppnId;
}
public String getPpnName() {
return ppnName;
}
public void setPpnName(String ppnName) {
this.ppnName = ppnName;
}
public String getPpnDescription() {
return ppnDescription;
}
public void setPpnDescription(String ppnDescription) {
this.ppnDescription = ppnDescription;
}
// Returns true if the call option structure has the same maturity as the PPN
public boolean isZeroCouponBonds() {
return isZeroCouponBonds;
}
public void setZeroCouponBonds(boolean isZeroCouponBonds) {
this.isZeroCouponBonds = isZeroCouponBonds;
}
//Returns true if this PPN uses a Constant Proportion Portfolio Insurance
//Structure which allows one to shift the Portfolio entity allocation
//towards a risker asset and a risk-free asset in response to change in IR
public boolean isCPPIStructured() {
return isCPPIStructured;
}
public void setCPPIStructured(boolean isCPPIStructured) {
this.isCPPIStructured = isCPPIStructured;
}
}
Here is a test stub class and the following schema that is generated
package org.anthonykuong.hibernate;
import java.util.HashSet;
import java.util.Set;
import org.anthonykuong.dto.Distributor;
import org.anthonykuong.dto.PrincipalProtectedNote;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ElementCollectionHibernateTest {
public static void main (String [] args){
Distributor distributor = new Distributor();
distributor.setName ("distributor_ppnName");
Set listofPPNs =new HashSet();
PrincipalProtectedNote ppn = new PrincipalProtectedNote();
ppn.setPpnId(1);
ppn.setPpnName("ppnName1");
PrincipalProtectedNote ppn2 = new PrincipalProtectedNote();
ppn2.setPpnId(2);
ppn2.setPpnName("ppnName2");
listofPPNs.add(ppn);
listofPPNs.add(ppn2);
distributor.setListofPPNs(listofPPNs);
Distributor distributor2 = new Distributor();
distributor2.setName ("a_distributor_ppnName");
Set listofPPN2s =new HashSet();
PrincipalProtectedNote ppn3 = new PrincipalProtectedNote();
ppn3.setPpnId(3);
ppn3.setPpnName("ppnName3");
PrincipalProtectedNote ppn4 = new PrincipalProtectedNote();
ppn4.setPpnId(4);
ppn4.setPpnName("ppnName4");
listofPPN2s.add(ppn3);
listofPPN2s.add(ppn4);
distributor2.setListofPPNs(listofPPN2s);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(distributor);
session.save(distributor2);
session.getTransaction().commit();
}
}

Hi,
How can we declare primary key for “DISTRIBUTOR_PPN_MAP” table.