Wednesday, January 4, 2012

One To Many Mapping
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;

public class College {

 private int collegeID;

 private String collegeName;

 private List<Student> students;

 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;

 public List<Student> getStudents() {
  return students;

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

public class Student {

 private int studentID;

 private String StudentName;

 private College college;

 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;

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

 public void setCollege(College college) { = college;
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();
  new SchemaExport(config).create(true, true);
  SessionFactory factory=config.buildSessionFactory();
  Session session=factory.getCurrentSession();
  College c1=new College();
  c1.setCollegeName("Richmond College Galle");
  Student stud1=new Student();
  stud1.setStudentName("Kanishka Dilshan");
  Student stud2=new Student();
  stud2.setStudentName("Madhuranga Sampath");;;;


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.


Post a Comment

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