This article is about hibernate practical example. Below is an example of hibernate with my sql using hibernate mapping files.
Here is the Student bean class with id and name properties.
- package beans;
- public class Student {
- private long id;
- private String name;
- /**
- * getters and setters
- * @return
- */
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Here is the student.hbm.xml file.
- <?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="beans.Student" table="student_master" schema="test">
- <id name="id" type="long" column="id">
- <generator />
- </id>
- <property name="name">
- <column name="name" />
- </property>
- </class>
- </hibernate-mapping>
Here is the hibernate.cfg.xml file. For this example mysql used as the data base.
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
- <property name="hibernate.connection.pool_size">10</property>
- <property name="show_sql">true</property>
- <property name="hibernate.hbm2ddl.auto">update</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.current_session_context_class">thread</property>
- <mapping resource="mappings/student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
Here is the Driver class. In the driver class we have created an instance of student and saved to the database.
- package driver;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import beans.Student;
- public class Driver {
- /**
- * main method
- */
- public static void main(String[] args) {
- Session session = null;
- try {
- //getting session factory instance
- SessionFactory sessionFactory = new Configuration().configure(
- "hibernate/hibernate.cfg.xml").buildSessionFactory();
- session = sessionFactory.openSession();
- session.beginTransaction();
- //student instance
- Student student = new Student();
- student.setName("Sanjay ");
- session.save(student);
- session.getTransaction().commit();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- } finally {
- session.flush();
- session.close();
- }
- }
- }
Overall project structure.
Image may be NSFW.
Clik here to view.![Hibernate Project Structure Hibernate Project Structure]()
Clik here to view.

Hibernate Project Structure
Your Suggestions Are Always Welcomed.
Image may be NSFW.Clik here to view.