Hibernate many-to-many Mapping with Hibernate Mapping (hbm.xml) file

Project View After Completion :
Hibernate Environment Setting

Right click over project on Project Explorer and goto properties->Java Build Path->Libraries

Add Required Hibernate jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.

For Details Read How to set Hibernate Environment


Student.java
 package blog.webideaworld.in;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class Student {  
      private int id;  
   private String name;  
   private int rollno;  
   private List<Books> books=new ArrayList<Books>();  
      public List<Books> getBooks() {  
           return books;  
      }  
      public void setBooks(List<Books> books) {  
           this.books = books;  
      }  
      public int getId() {  
           return id;  
      }  
      public void setId(int id) {  
           this.id = id;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public int getRollno() {  
           return rollno;  
      }  
      public void setRollno(int rollno) {  
           this.rollno = rollno;  
      }  
 }  

Books.java
 package blog.webideaworld.in;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class Books {  
      private int bookid;  
      private String bookname;  
   private List<Student> student=new ArrayList<Student>();  
      public List<Student> getStudent() {  
           return student;  
      }  
      public void setStudent(List<Student> student) {  
           this.student = student;  
      }  
      public int getBookid() {  
           return bookid;  
      }  
      public void setBookid(int bookid) {  
           this.bookid = bookid;  
      }  
      public String getBookname() {  
           return bookname;  
      }  
      public void setBookname(String bookname) {  
           this.bookname = bookname;  
      }  
 }  

ExeMain.java
It is working as Persistent class for Hibernate and also this class we are using to store object of Student and Books class using Hibernate Framework.
 package blog.webideaworld.in;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.cfg.Configuration;  
 public class ExeMain {  
      public static void main(String args[]){  
           Student st =new Student();  
           st.setName("jack");  
        st.setRollno(44);  
        Books bk1=new Books();  
        bk1.setBookname("Let us C");  
        Books bk2=new Books();  
        bk2.setBookname("Let us C++");  
        st.getBooks().add(bk1);  
        st.getBooks().add(bk2);  
        bk1.getStudent().add(st);  
        bk2.getStudent().add(st);  
        @SuppressWarnings("deprecation")  
        SessionFactory sessionFactory     =new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();  
        Session session=sessionFactory.openSession();  
        session.beginTransaction();  
        session.save(st);  
        session.save(bk1);  
        session.save(bk2);  
        session.getTransaction().commit();  
      }  
 }  

hibernate.cfg.xml
This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information.
 <?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.dialect">org.hibernate.dialect.OracleDialect</property>  
   <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>  
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
   <property name="hibernate.connection.username">hr</property>  
   <property name="hibernate.connection.password">hr</property>  
   <property name="hibernate.show_sql">true</property>  
   <property name="hibernate.hbm2ddl.auto">create</property>  
   <mapping resource="Student.hbm.xml"/>  
   <mapping resource="Books.hbm.xml"/>  
  </session-factory>  
 </hibernate-configuration>  

Student.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="blog.webideaworld.in.Student" table="Student">  
    <id name="id">  
      <generator class="increment"/>  
    </id>  
    <property name="name" column="Name"/>  
    <property name="rollno" column="Rollno"/>  
   <list name="books" inverse="false">  
   <key column="id"/>  
   <list-index column="list_id"/>  
   <many-to-many column="book_id" class="blog.webideaworld.in.Books"/>  
   </list>  
   </class>  
 </hibernate-mapping>  

Books.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="blog.webideaworld.in.Books" table="Books">  
    <id name="bookid">  
      <generator class="increment"/>  
    </id>  
    <property name="bookname" column="bookname"/>  
    <list name="student" inverse="true">  
    <key column="bookid"/>  
    <list-index column="list_id"/>  
    <many-to-many class="blog.webideaworld.in.Student"/>  
    </list>  
    </class>  
 </hibernate-mapping>  

To Run this Project Right Click inside ExeMain.java, select Run As -> Java Application

Download Code Link 1
Download Code Link 2

Output:

Student Table:





Books Table: 





Student_books Table:













More Hibernate Topics :

No comments: