Hibernate Tutorial

HibernateUtil.java

package com.javatips.util;

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("Initial SessionFactory creation failed." + ex);
     
throw new ExceptionInInitializerError(ex);
   
}
  }

 
public static SessionFactory getSessionFactory() {
   
return sessionFactory;
 
}
}

Create a Student Object(Student.java)

package com.javatips.student;

public class Student implements java.io.Serializable {

 
private long studentId;
 
private String studentName;

 
public void setStudentId(long studentId) {
   
this.studentId = studentId;
 
}

 
public long getStudentId() {
   
return studentId;
 
}

 
public void setStudentName(String studentName) {
   
this.studentName = studentName;
 
}

 
public String getStudentName() {
   
return studentName;
 
}

}

Student.hbm.xml

This is the hibernate mapping file used to map the domain (entity class) with database table

<?xml version="1.0"?>
<!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.javatips.student.Student" table="StudentDetails">
        <meta attribute="class-description">
            This class contains the student details.
        </meta>
        <id name="studentId" type="long" column="STUDENT_ID">
            <generator class="native" />
        </id>
        <property name="studentName" type="string" column="STUDENT_NAME"
            not-null="true" />
    </class>
</hibernate-mapping>

Package Structure

Hibernate Tutorial Package Structure




Previous 1 2 3 Next








Your email address will not be published. Required fields are marked *