Hibernate 4 Annotation Example

In this tutorial you will learn about how to make a project in Hibernate using Annotation.

Hibernate 4 Annotation Example

Hibernate 4 Annotation Example

In this tutorial you will learn about how to make a project in Hibernate using Annotation.

To create an example of Hibernate using annotation reader should have aware with some basic terms. You can know about these terms from here. After letting know the basic terms that are useful for creating a project proceed from here. In the basic terms you will have read that the POJO class will be changed after using annotation lets see the changes occurred in POJO class.

Here I have created POJO java class named Person.java using the Annotations (may be called Entity class).

package roseindia.net;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name= "person")
public class Person
{
@Id
@GeneratedValue
@Column(name = "Id")
private int id;

@Column(name = "Name")
private String name;

public Person()
{

}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Here is the video instruction of writing and running the Hibernate Annotation example:

You can download the source code of the above video tutorial from the page Hibernate 4 Annotations Tutorial.

Next I have created class named PersonFactory that helps in to create a SessionFactory from the Hibernate configuration file.

package roseindia.net;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class PersonFactory {
private static ServiceRegistry serviceRegistry;

private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

Created a hibernate.cfg.xml file that the hibernate utilize to create a connection pool and the required environment setup.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/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://192.168.10.13:3306/data
</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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="roseindia.net.Person" />
</session-factory>
</hibernate-configuration>

Finally develop the code by writing a java class named PersonDetail that persist the data to the database.

PersonDetail.java

package roseindia.net;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class PersonDetail
{
	private static SessionFactory sessionFactory = null;
	public static void main(String[] args)
	{		
		Session session = null;
		try 
		 {
			try
			{
				PersonFactory factory = new PersonFactory();
				sessionFactory = factory.getSessionFactory();
				session = sessionFactory.openSession();
				Person person = new Person();
				System.out.println("Inserting Record");
				Transaction tx = session.beginTransaction();
				person.setId(2);
				person.setName("Roseindia");
				session.save(person);
				tx.commit();
				System.out.println("Done");
			}
			catch (Exception e)
			{
				System.out.println(e.getMessage());
			}
			}
			finally
			{
				session.close();
			}
		}
	}

Output :

When you will execute the PersonDetail class following output will displayed as :

1. Output on console will be as :

2. Output of data that will be saved to the database table after executing the code successfully.

Download Source Code