Thursday, 15 August 2013

Simple Hibernate Example (Annotation)


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

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 with Hibernate annotations

package com.pretech;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Table(name = "studentdetails", catalog = "hibernateschema")
public class Student implements Serializable {
    private long id;
    private String name;
    private String standard;
    @Column(name = "standard", unique = true, nullable = false, length = 20)
    public String getStandard() {
        return standard;
    public void setStandard(String standard) {
        this.standard = standard;
    @Column(name = "ID", unique = true, nullable = false)
    public long getId() {
        return id;
    @Column(name = "name", unique = true, nullable = false, length = 10)
    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 Configuration file (hibernate.cfg.xml)

Note: here we are adding the Mapping class instead of mapping xml file

<!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 class="com.pretech.Student"></mapping>

4. 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("9th Standard");;
        System.out.println("Updated to Student details table");
        if (session != null)


Hibernate: insert into hibernateschema.studentdetails (name, standard) values (?, ?)
Updated to Student details table