Hibernate Many to Many Example

Hibernate Many to Many Example with Oracle

Tools and Technologies used in this Example.
1. Eclipse
2. Oracle 9i
3. Hibernate

Steps:
1. Create table to establish many to many relationship.

  SQL> create table student(sid number(5) primary key, sname varchar2(20));

  Table created.

  SQL> create table course(cid number(5) primary key, cname varchar2(20));

  Table created.


  SQL> create table student_course(sid number(5),cid number(5),primary key(sid,cid));

  Table created.

 -> Establish many to many relationship by using following alter table.

  SQL> alter table student_course add foreign key(sid) references student(sid);

  Table altered.

  SQL> alter table student_course add foreign key(cid) references course(cid);

  Table altered.

  SQL> Commit;

2. Create Java Project and add all jar files related to Project.
     Hibernate 3 jars
     ojdbc14.jar



3. Create Hibernate Model classes , Mapping files and Configuration files
      To know how to create Model, Mapping and Configuration files click HERE .

hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="sessionFactory">
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">****YourPassword***</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.username">YourUserName</property>
        <property name="hibernate.default_schema">HIB</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
    <mapping resource="com/javafws/blog/Student.hbm.xml" />
    <mapping resource="com/javafws/blog/Course.hbm.xml" />
    </session-factory>
</hibernate-configuration>



Student.java


package com.javafws.blog;

import java.util.HashSet;
import java.util.Set;

/**
 * Student generated by hbm2java
 */
public class Student implements java.io.Serializable {

private int sid;
private String sname;
private Set<Course> courses = new HashSet<Course>(0);

public Student() {
}

public Student(int sid) {
this.sid = sid;
}

public Student(int sid, String sname, Set<Course> courses) {
this.sid = sid;
this.sname = sname;
this.courses = courses;
}

public int getSid() {
return this.sid;
}

public void setSid(int sid) {
this.sid = sid;
}

public String getSname() {
return this.sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public Set<Course> getCourses() {
return this.courses;
}

public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}


Course.java



package com.javafws.blog;

import java.util.HashSet;
import java.util.Set;

/**
 * Course generated by hbm2java
 */
public class Course implements java.io.Serializable {

private int cid;
private String cname;
private Set<Student> students = new HashSet<Student>(0);

public Course() {
}

public Course(int cid) {
this.cid = cid;
}

public Course(int cid, String cname, Set<Student> students) {
this.cid = cid;
this.cname = cname;
this.students = students;
}

public int getCid() {
return this.cid;
}

public void setCid(int cid) {
this.cid = cid;
}

public String getCname() {
return this.cname;
}

public void setCname(String cname) {
this.cname = cname;
}

public Set<Student> getStudents() {
return this.students;
}

public void setStudents(Set<Student> students) {
this.students = students;
}

}


Student.hbm.xml

<?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 May 2, 2013 5:31:40 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.javafws.blog.Student" table="STUDENT">
        <id name="sid" type="int">
            <column name="SID" precision="5" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="sname" type="string">
            <column name="SNAME" length="20" />
        </property>
        <set name="courses" table="STUDENT_COURSE" inverse="false" cascade="all" lazy="true" fetch="select">
            <key>
                <column name="SID" precision="5" scale="0" not-null="true" />
            </key>
            <many-to-many entity-name="com.javafws.blog.Course">
                <column name="CID" precision="5" scale="0" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


Course.hbm.xml.

<?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 May 2, 2013 5:31:40 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.javafws.blog.Course" table="COURSE">
        <id name="cid" type="int">
            <column name="CID" precision="5" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="cname" type="string">
            <column name="CNAME" length="20" />
        </property>
        <set name="students" table="STUDENT_COURSE" inverse="true" cascade="all" lazy="true" fetch="select">
            <key>
                <column name="CID" precision="5" scale="0" not-null="true" />
            </key>
            <many-to-many entity-name="com.javafws.blog.Student">
                <column name="SID" precision="5" scale="0" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


MyApp.java - Main Application

package com.javafws.blog;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class MyApp {

public static void main(String[] args) {
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
Session hSession= sf.openSession();
Transaction txt = hSession.beginTransaction();
Student student = new Student();
student.setSid(2);
student.setSname("Prasad");
Course course = new Course();
course.setCid(3);
course.setCname("MCA");
Set<Course> set = new HashSet<>();
set.add(course);
student.setCourses(set);
hSession.save(student);
txt.commit();
hSession.close();
}
}


Finally run your project.

Tags:

0 Responses to “Hibernate Many to Many Example”

Post a Comment

Thanks for your comments

Subscribe

© 2014 Java Frameworks. All rights reserved.
Designed by Blogger