Saturday 21 July 2012

Step by Step Example for Many-to-Many in hibernate

           In many-to-many Association, one parent Pojo class object points one or more child pojo class object. Similarly one child pojo class object points one or more parent pojo class objects.


           The Relationship between Student and Courses is many-to-many relationship. because one or multiple Students can be their in a one course. one or multiple Courses can be studied by students

In Many-Many Relations uni-directional and bi-directional are possible

Sample Example for Many-to-Many In Uni-Directional

         *When we apply many-to-many relations in hibernate we need to create 3 tables.

Tables
* for student table 
create table student
(
studentid number(3) primary key,
studentname varchar2(10),
marks number(4)
);

* for courses table
create table courses
(
courseid number(3) primary key,
coursename varchar2(10)
);

*for students_courses table
create table students_courses
(
courseid number(3) references course(courseid),
studentid number(3) references student(studentid)
);

*POJO class for student

import java.util.HashSet;
import java.util.Set;
public class Student {
private int studentId;
private String studentName;
private Set <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;
}
public Set<Course> getCourse() {
return course;
}
public void setCourse(Set<Course> course) {
this.course = course;
}
}


* For Course POJO class

import java.util.Set;
public class Course {
private int courseId;
private String courseName;
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;
}
}


Mapping Files

*For Student mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 21, 2012 11:27:47 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="Student" table="STUDENT">
        <id name="studentId" type="int">
            <column name="STUDENTID" />
          
        </id>
        <property name="studentName" type="java.lang.String">
            <column name="STUDENTNAME" />
        </property>
        <set name="course" table="studentcourse" cascade="all" >
            <key column="studentId"/>
             
         
            <many-to-many column="courseID" class="Course" />
        </set>
    </class>
</hibernate-mapping>



* For courses mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 21, 2012 11:27:47 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="Course" table="COURSE">
        <id name="courseId" type="int">
            <column name="COURSEID" />         
        </id>
        <property name="courseName" type="java.lang.String">
            <column name="COURSENAME" />
        </property>       
    </class>
</hibernate-mapping>



Configuration file



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system123</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="Student.hbm.xml"/>
<mapping resource="Courses.hbm.xml"/>
</session-factory>
</hibernate-configuration>

* For Client Application

import java.util.HashSet;
import java.util.Set;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session ses=sf.openSession();
Set<Course>s1=new HashSet<Course>();
Course c=new Course();
c.setCourseId(1);
c.setCourseName("Java");
Course c1=new Course();
c1.setCourseId(2);
c1.setCourseName("PHP");
   s1.add(c);
   s1.add(c1);
   
   Student s=new Student();
   s.setStudentId(0);
   
   s.setStudentName("Raja");
   Student stu=new Student();
   stu.setStudentId(1);
   stu.setStudentName("kavi");
   
   s.setCourse(s1);
   stu.setCourse(s1);
   
   Transaction t=ses.beginTransaction();
   ses.save(s);
   ses.save(stu);
   t.commit();
}

}




For Downloading Applications 

________________________________________________________________________________


Sample Example for Many-to-Many In Bi-Directional

---> In Bi-Directional we can access the child class object through parent class object vice versa.

For Downloading Applications:



Send me feedback and suggestions...

your feedback will help to me as well as others...

Thanks





No comments:

Post a Comment