Wednesday, January 4, 2012

One To Many Mapping


College.java
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class College {

 private int collegeID;

 private String collegeName;

 private List<Student> students;

 @Id
 @GeneratedValue
 public int getCollegeID() {
  return collegeID;
 }

 public void setCollegeID(int collegeID) {
  this.collegeID = collegeID;
 }

 public String getCollegeName() {
  return collegeName;
 }

 public void setCollegeName(String collegeName) {
  this.collegeName = collegeName;
 }

 @OneToMany(targetEntity=Student.class,mappedBy="college",
   cascade=CascadeType.ALL,fetch=FetchType.EAGER)
 public List<Student> getStudents() {
  return students;
 }

 public void setStudents(List<Student> students) {
  this.students = students;
 }
}
Student.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Student {

 private int studentID;

 private String StudentName;

 private College college;

 @Id
 @GeneratedValue
 public int getStudentID() {
  return studentID;
 }

 public void setStudentID(int studentID) {
  this.studentID = studentID;
 }

 public String getStudentName() {
  return StudentName;
 }

 public void setStudentName(String studentName) {
  StudentName = studentName;
 }

 @ManyToOne
 @JoinColumn(name="college_id ")
 public College getCollege() {
  return college;
 }

 public void setCollege(College college) {
  this.college = college;
 }
}
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(College.class);
  config.addAnnotatedClass(Student.class);
  config.configure("hibernate.cfg.xml");
  
  new SchemaExport(config).create(true, true);
  SessionFactory factory=config.buildSessionFactory();
  Session session=factory.getCurrentSession();
  session.beginTransaction();
  
  College c1=new College();
  c1.setCollegeName("Richmond College Galle");
  
  Student stud1=new Student();
  stud1.setCollege(c1);
  stud1.setStudentName("Kanishka Dilshan");
  
  Student stud2=new Student();
  stud2.setCollege(c1);
  stud2.setStudentName("Madhuranga Sampath");
  
  
  session.save(c1);
  session.save(stud1);
  session.save(stud2);
  
  session.getTransaction().commit();
 }

}

New Annotations :
  • @OneToMany(targetEntity=Student.class,mappedBy="college", cascade=CascadeType.ALL,fetch=FetchType.EAGER)   :  This annotation goes to the "one side" target entity is the "many side" class.  "mappedby" parameter denotes the attribute in the "many side" which is used to make the connection between "one side".
  • @ManyToOne : This annotations goes to the "many side". It is used to denoted the attribute which is used to make the association between two classes.
  • @JoinColumn(name="college_id ") : We can name the column of the relational table by using this annotation.
Results :

Note : Default fetch type for the one to many mapping is LAZY because it may consume lots of memory to retrieve and contain number of objects  from the table. Anyway I have overridden it here to demonstration.

0 comments:

Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis