In this example we are describing one-to-many relation. There are two table one is Faculty and other is Subject.
Faculty Table
Facultyi_Id | Faculty_Name |
---|---|
1 | Neelesh |
2 | Remu |
3 | Kripa |
Subject Table
Subject_Id | Subject_Name |
---|---|
1 | c |
2 | c++ |
3 | Java |
4 | VB |
5 | Dotnet |
Bridge Table i.e Facult_Subject
Faculty_Id | Subject_Id |
---|---|
1 | 1 |
1 | 2 |
2 | 3 |
2 | 4 |
2 | 5 |
In a College suppose different subjects can assign to one faculty but that same subject can't assign to different faculty. This is an one-to-many relationship. In this example we have shown facuty_id no 1 has assigned two subjects i.e c and c++, faculty_id no 2 - one subject java and so on. The Bridge table contains Faculty_id and Subject_id. This can be achieve by many-to-many mapping but we have to add one more attribute i,e unique="true"
TO achiving this relation we have to follow these steps
Step1: Create Faculty and Subject class
Faculty.java
package com.bullraider.onetomany; import java.util.HashSet; import java.util.Set; public class Faculty { private long id; private String name; private Set<Subject> subjects=new HashSet<Subject>(0); public Faculty(String name) { super(); this.name = name; } public Set<Subject> getSubjects() { return subjects; } public void setSubjects(Set<Subject> subjects) { this.subjects = subjects; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Subject.java
package com.bullraider.onetomany; public class Subject { private long id; private String name; public Subject(String name) { super(); this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Step2: Create Faculty.hbm.xml and Subject.hb.xml
Faculty.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bullraider.onetomany.Faculty" table="Faculty"> <meta attribute="class-description"> This class contains Faculties details. </meta> <id name="id" type="long" column="id"> <generator class="native" /> </id> <property name="name" type="string" not-null="true" /> <set name="subjects" table="subject_faculty" cascade="all" > <key column="faculty_Id" /> <many-to-many column="subject_Id" class="com.bullraider.onetomany.Subject" unique="true"/> </set> </class> </hibernate-mapping>
Subject.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bullraider.onetomany.Subject" table="subject"> <meta attribute="class-description"> This class contains policy details. </meta> <id name="id" type="long" column="id"> <generator class="native" /> </id> <property name="name" type="string" not-null="true" /> </class> </hibernate-mapping> Step3: Create Configuration file <?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> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property> <property name="hibernate.connection.username">scott</property> <property name="connection.password">tiger</property> <property name="connection.pool_size">1</property> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create-drop</property> <mapping resource="com/bullraider/manytomany/Subject.hbm.xml"/> <mapping resource="com/bullraider/manytomany/Faculty.hbm.xml"/> </session-factory> </hibernate-configuration>
step4: Create HibernateUtil class to get SessionFactory Object
package com.bullraider.manytomany; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (Throwable ex) { System.err.println("SessionFactory creation failed" + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
step5: Create One Main Class for adding opertions.
package com.bullraider.onetomany; import java.util.HashSet; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.bullraider.onetomany.util.HibernateUtil; public class Main { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Set<Subject> subject= new HashSet<Subject>(); subject.add(new Subject("Java")); subject.add(new Subject("C++")); Faculty fc1=new Faculty("Srinivas"); fc1.setSubjects(subject); session.save(fc1); transaction.commit(); System.out.println("Records inserted sucessessfully"); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }
Note:
<set name="subjects" table="subject_faculty" cascade="all" > <key column="faculty_Id" /> <many-to-many column="subject_Id" class="com.bullraider.o2m.Subject" unique="true"/> </set>
Here we are creating Bridge table "subject_faculty" having attribute "faculty_Id" ,"subject_Id.