Event.java
import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Event { private int eventID; private String eventName; private List<Delegate> delegates = new ArrayList<Delegate>(); @Id @GeneratedValue public int getEventID() { return eventID; } public void setEventID(int eventID) { this.eventID = eventID; } public String getEventName() { return eventName; } public void setEventName(String eventName) { this.eventName = eventName; } @ManyToMany @JoinTable(name="Join_Delegate_event", joinColumns={@JoinColumn(name="eventID")}, inverseJoinColumns={@JoinColumn(name="delegateID")} ) public List<Delegate> getDelegates() { return delegates; } public void setDelegates(List<Delegate> delegates) { this.delegates = delegates; } }
Delegate.java
import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Delegate { private int delegateID; private String delegateName; private List<Event> events = new ArrayList<Event>(); @Id @GeneratedValue public int getDelegateID() { return delegateID; } public void setDelegateID(int delegateID) { this.delegateID = delegateID; } public String getDelegateName() { return delegateName; } public void setDelegateName(String delegateName) { this.delegateName = delegateName; } @ManyToMany @JoinTable(name="Join_Delegate_event", joinColumns={@JoinColumn(name="delegateID")}, inverseJoinColumns={@JoinColumn(name="eventID")} ) public List<Event> getEvents() { return events; } public void setEvents(List<Event> events) { this.events = events; } }Test.java
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class Test { /** * @param args */ public static void main(String[] args) { AnnotationConfiguration config=new AnnotationConfiguration(); config.addAnnotatedClass(Event.class); config.addAnnotatedClass(Delegate.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); SessionFactory factory=config.buildSessionFactory(); Session session=factory.getCurrentSession(); session.beginTransaction(); Delegate del1=new Delegate(); del1.setDelegateName("Kanishka Dilshan"); Delegate del2=new Delegate(); del2.setDelegateName("Sajith Athukorala"); Delegate del3=new Delegate(); del3.setDelegateName("Vinod De Silva"); Delegate del4=new Delegate(); del4.setDelegateName("Ravindy Madhuwan"); Event ev1=new Event(); ev1.setEventName("Java"); Event ev2=new Event(); ev2.setEventName("C++"); Event ev3=new Event(); ev3.setEventName("Assembly"); ev1.getDelegates().add(del1); ev1.getDelegates().add(del2); ev1.getDelegates().add(del3); ev2.getDelegates().add(del2); ev2.getDelegates().add(del4); ev3.getDelegates().add(del3); session.save(del1); session.save(del2); session.save(del3); session.save(del4); session.save(ev1); session.save(ev2); session.save(ev3); session.getTransaction().commit(); } }
New Annotations :
- @ManyToMany : denotes that the entity has many to many relationship
- @JoinTable(name="Join_Delegate_event",
joinColumns={@JoinColumn(name="eventID")},
inverseJoinColumns={@JoinColumn(name="delegateID")}
) : Here the name parameter denotes the table name which is created to maintain many to many relationship.
0 comments:
Post a Comment