Example of @ManyToOne in Hibernate
May 21, 2013
@ManyToOne in hibernate creates the association between two entities in such a way that more than one entity can be associated with single entity. These is called Many-to-one association in hibernate. The example of Many-to-one can be that more than one student may associates with single college, Many citizen lives in single country etc. Syntax is
@ManyToOne( cascade = CascadeType.ALL ) @JoinColumn(name="cloumnId")
Student.java
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="student") public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @Column(name="std_id") private int studentId; @Column(name="std_name") private String studentName; @ManyToOne( cascade = CascadeType.ALL ) @JoinColumn(name="col_id") private College college; public Student(int studentId,String studentName,College college){ this.studentId=studentId; this.studentName=studentName; this.college=college; } public Student(){ } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public College getCollege() { return college; } public void setCollege(College college) { this.college = college; } }
College.java
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="college") public class College implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="col_id") private int colId; @Column(name="col_name") private String collegeName; @Column(name="student_num") private int numStudent; public College(){ } public College(int colId,String collegeName,int numStudent){ this.colId=colId; this.collegeName=collegeName; this.numStudent=numStudent; } public int getColId() { return colId; } public void setColId(int colId) { this.colId = colId; } public String getCollegeName() { return collegeName; } public void setCollegeName(String collegeName) { this.collegeName = collegeName; } public int getNumStudent() { return numStudent; } public void setNumStudent(int numStudent) { this.numStudent = numStudent; } }
HibernateUtil.java
package com.concretepage.util; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.College; import com.concretepage.persistence.Student; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { Properties prop= new Properties(); prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate"); prop.setProperty("hibernate.connection.username", "root"); prop.setProperty("hibernate.connection.password", ""); prop.setProperty("hibernate.hbm2ddl.auto", "update"); prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect"); concreteSessionFactory = new AnnotationConfiguration() .addPackage("com.concretepage.persistence") .addProperties(prop) .addAnnotatedClass(College.class) .addAnnotatedClass(Student.class) .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return concreteSessionFactory.openSession(); } public static void main(String... args){ Session session=getSession(); session.beginTransaction(); College college=new College(1,"BHU",1000); Student s= new Student(1,"Ram",college); session.persist(s); Student s2= new Student(2,"Shyam",college); session.persist(s2); session.getTransaction().commit(); } }
Output