Hibernate - SPLessons

Chapter 33

Hibernate Basic Application

Hibernate Basic Application

Hibernate Basic Application, As the earlier SPlesson tutorial explained regarding hibernate basics where this tutorial covered files which are required to execute a Hibernate Basic Application, following are the required files needs to be added while executing the application.

Hibernate configuration file will have the information regarding the connections to MySQL , URL, driver names, user name and password of the MySQL. Hibernate mapping file will have the database table name and consists of generator classes.


Following is the Hibernate Basic Application.


package com.abc;
public class Employee {
private int id;
private String firstName,lastName;

public int getId() {
	return id;
public void setId(int id) {
	this.id = id;
public String getFirstName() {
	return firstName;
public void setFirstName(String firstName) {
	this.firstName = firstName;
public String getLastName() {
	return lastName;
public void setLastName(String lastName) {
	this.lastName = lastName;

Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner,one can have control over the public interface

package com.abc;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class StoreData {
public static void main(String[] args) {
	//creating configuration object
	Configuration cfg=new Configuration();
	cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
	//creating seession factory object
	SessionFactory factory=cfg.buildSessionFactory();
	//creating session object
	Session session=factory.openSession();
	//creating transaction object
	Transaction t=session.beginTransaction();
	Employee e1=new Employee();
	session.persist(e1);//persisting the object
	t.commit();//transaction is commited
	System.out.println("your data has been stored successfully, please check in your data base.");

persist() is like recovery (with exchange) and it adds the entity object to the persistent context, so any further changes are followed. In the event that the article properties are changed before the exchange is commited or session is flushed, it will likewise be spared into database.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

          <class name="com.abc.Employee" table="emp1000">
          <id name="id">
          <generator class="assigned"></generator>
          <property name="firstName"></property>
          <property name="lastName"></property>

The generator classsubelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->

        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping resource="employee.hbm.xml"/>

Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.


Output will be as follows. While compiling the application following output will be displayed, Information will be stored in database.

The following output will be displayed in data base browser(SQLyog).


  • Hibernate Basic Application – One Hibernate application may have more mapping file.
  • To execute the application server is not needed.
  • The configuration file will have the data about connecting information.