In this example we will see how to establish many to many relationship in hibernate. To explain this, lets take two classes
Student and Course. A student can enroll in any number of courses and a course can be enrolled by any number of students.
So this makes a many to many relationship. To achieve this we can make use of the annotation "@ManyToMany". Below code
is sample program of the many to many relation ship.
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">hr</property> <property name="connection.password">hr</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- Names the annotated entity class --> <mapping class="com.ram.dao.Student"/> <mapping class="com.ram.dao.Course"/> </session-factory> </hibernate-configuration>
File: com.ram.dao.Student.java
package com.ram.dao; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Student { @Id @GeneratedValue private int studentId; private String studentName; @ManyToMany private Collectioncourse = new ArrayList (); public Collection getCourse() { return course; } public void setCourse(Collection course) { this.course = course; } 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; } }
File: com.ram.dao.Course.java
package com.ram.dao; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Course { @Id @GeneratedValue private int courseId; private String courseName; private int courseDays; @ManyToMany private Collectionstudent = new ArrayList (); public Collection getStudent() { return student; } public void setStudent(Collection student) { this.student = student; } public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public int getCourseDays() { return courseDays; } public void setCourseDays(int courseDays) { this.courseDays = courseDays; } }
File: com.ram.hibernate.ManyToManyExample.java
package com.ram.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.ram.dao.Course; import com.ram.dao.Student; public class ManyToManyExample { public static void main(String[] args) { Student student1 = new Student(); student1.setStudentName("Ram"); Student student2 = new Student(); student2.setStudentName("Veena"); Course course1 = new Course(); course1.setCourseName("Hibernate3"); course1.setCourseDays(10); Course course2 = new Course(); course2.setCourseName("HTML5"); course2.setCourseDays(5); course1.getStudent().add(student1); course1.getStudent().add(student2); course2.getStudent().add(student1); course2.getStudent().add(student2); student1.getCourse().add(course1); student1.getCourse().add(course2); student2.getCourse().add(course1); student2.getCourse().add(course2); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(student1); session.save(student2); session.save(course1); session.save(course2); session.getTransaction().commit(); session.close(); } }
No comments:
Post a Comment