Saturday, February 2, 2013

Hibernate many-to-one 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
 public class Student {  
      private int id;  
   private String name;  
   private int rollno;  
      public int getId() {  
           return id;  
      public void setId(int id) {  
  = id;  
      public String getName() {  
           return name;  
      public void setName(String name) {  
  = name;  
      public int getRollno() {  
           return rollno;  
      public void setRollno(int rollno) {  
           this.rollno = rollno;  
 public class Books {  
      private int bookid;  
      private String bookname;  
   private Student student;  
      public Student getStudent() {  
           return student;  
      public void setStudent(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;  
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.
 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();  
        Books bk1=new Books();  
        bk1.setBookname("Let us C");  
        Books bk2=new Books();  
        bk2.setBookname("Let us C++");  
        SessionFactory sessionFactory     =new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();  
        Session session=sessionFactory.openSession();  
        session.beginTransaction(); ; ; ;  

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" "">  
   <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="">create</property>  
   <mapping resource="Student.hbm.xml"/>  
   <mapping resource="Books.hbm.xml"/>  

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">  
  <class name="" table="Student">  
    <id name="id">  
      <generator class="increment"/>  
    <property name="name" column="Name"/>  
    <property name="rollno" column="Rollno"/>  

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">  
  <class name="" table="Books">  
    <id name="bookid">  
      <generator class="increment"/>  
    <property name="bookname" column="bookname"/>  
    <many-to-one name="student" class="" column="stu_id"/>  

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

Download Code Link 1
Download Code Link 2

Student Table: 

Books Table: 


No comments:

Popular Posts