Wednesday, 14 August 2013

Simple Hibernate Example (XML Mapping)


This is one simple Hibernate example to insert java entity into MySql database.

Software Used

Hibernate 3

Java 1.7

MySql 5

Eclipse Juno

1. Project Structure

Create a Java project and update Hibernate jars and Mysql driver jar in to build path. (Download Hibernate Jar , MySql Jar)


2. Create an Entity class

package com.pretech;


public class Student implements Serializable {
    private long id;
    private String name;
    private String standard;

    public String getStandard() {
        return standard;

    public void setStandard(String standard) {
        this.standard = standard;

    public long getId() {
        return id;

    public String getName() {
        return name;

    public void setId(long string) {
        id = string;

    public void setName(String string) {
        name = string;

    public String toString() {
        return name;

3. Create Hibernate Mapping file (Student.hbm.xml)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
<hibernate-mapping >
    <class name="com.pretech.Student" table="StudentDetails" >
         <id name="id" type="long" column ="ID">
        <generator class="increment"/>
         <property name="name" column="name" not-null="true"/>
         <property name="standard" column="standard" />

4. Create Hibernate Configuration file (hibernate.cfg.xml)

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    <session-factory name="studentFactory">
            <property name="connection.driver_class">
        <property name="connection.url">
        <property name="connection.username">
        <property name="connection.password">
            <property name="connection.pool_size">5</property>
            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
             <property name="">update</property>
        <mapping resource="Student.hbm.xml"/>

5. Create a Main program to save Student entity

package com.pretech;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class StudentMain {
    private static SessionFactory sessionFactory;
    public static void main(String args[]) throws Exception {
        try {
            sessionFactory = new Configuration().configure("hibernate.cfg.xml")
        } catch (Exception e) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Student student = new Student();
        student.setStandard("10th Standard");;
        System.out.println("Updated to Student details table");
        if (session != null)

6. Output

Hibernate: select max(ID) from StudentDetails
Hibernate: insert into StudentDetails (name, standard, ID) values (?, ?, ?)
Updated to Student details table