Searching...
Wednesday, 14 August 2013

Simple Hibernate Example (XML Mapping)

23:10

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)

image

2. Create an Entity class

package com.pretech;

import java.io.*;

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"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
    <class name="com.pretech.Student" table="StudentDetails" >
         <id name="id" type="long" column ="ID">
        <generator class="increment"/>
         </id>
         <property name="name" column="name" not-null="true"/>
         <property name="standard" column="standard" />
    </class>
</hibernate-mapping>

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

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="studentFactory">
            <property name="connection.driver_class">
                 com.mysql.jdbc.Driver
            </property>
        <property name="connection.url">
             jdbc:mysql://localhost:3306/hibernateschema
        </property>
        <property name="connection.username">
             root
        </property>
        <property name="connection.password">
            root
        </property>
            <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="hbm2ddl.auto">update</property>
        <mapping resource="Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

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")
                    .buildSessionFactory();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Student student = new Student();
        student.setName("Vinod");
        student.setStandard("10th Standard");
        session.save(student);
        tx.commit();
        System.out.println("Updated to Student details table");
        if (session != null)
            session.close();
    }
}

6. Output

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

image

0 comments: