Pages Navigation Menu

Coding is much easier than you think

Generic Hibernate Application Requirements – Annotation

Hibernate Generic Requirment
The objective of this example is to understand the general requirement to be followed in creating any hibernate application in Java. You may want to look at Hibernate Installation/Setup on Eclipse IDE article if Hibernate is not installed already on your system.
In general any hibernate application, must have the following 3 files,
1) Configuration XML – This file will be used to store database connection information and schema level settings.
2) Entity Class – This class will be java POJO having hibernate annotations.
3) Java file to access this configuration file and write our logic
These files are the minimum requirement to run any hibernate application, in case of complex application we may require many Entity classes.
Note : Number of Entity classes = Number of Tables in database

Hibernate Configuration file

In this mapping file Hibernate gets to know about the type of database and its connection properties as well as about the Entity class.
File: hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"


	<!-- Database connection settings -->
	<property name="hibernate.connection.driver_class">
	<property name="hibernate.connection.username">system</property>
	<property name="hibernate.connection.password">admin</property>
	<property name="hibernate.connection.url">

	<!-- SQL dialect -->
	<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

	<!-- Echo all executed SQL to sysout -->
	<property name="show_sql">true</property>

	<!-- Drop and re-create the database schema on startup -->
	<property name="">update</property>
	<!-- Map Entity Class -->
    <mapping class="entity.StudentEntity"></mapping>



Entity class

The entity class is where the Hibernate mapping comes into play, this class tells Hibernate what table in the database it has to access, and what columns in that table it should use. For each member variable defined in Entity class you must create a getter and a setter.

package entity;

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

@Table(name = "STUDENT")
public class StudentEntity {

	@Column(name = "ID")
	private int id;
	@Column(name = "NAME")
	private String name;
	@Column(name = "DEPARTMENT")
	private String department;
	@Column(name = "COLLEGE")
	private String college;

// Create Getters and Setters

As shown above, the mapping file contains several annotations, listed below are its role:

  • @Entity annotation marks this class as an entity bean
  • @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database.

    The @Table annotation provides four attributes, allowing you to override the name of the table, its catalogue, and its schema, and enforce unique constraints on columns in the table. For now we are using just table name which is STUDENT.

  • @Id Annotations:– In hibernate each entity bean should have a primary key, which you annotate on the class with the @Id annotation. The primary key can be a single field or a combination of multiple fields depending on your table structure.
  • @Column, The @Column annotation is used to specify the details of the column to which a field or property will be mapped. Here the name attribute in @Column permits the name of the column to be explicitly specified.

In our next article we shall learn about Generic Steps to be followed to use Hibernate in any Java application, going forward using this two articles we shall implement Hello World example of Hibernate 4 in Eclipse

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.