Hibernate Notes


  • The Hibernate project has the structure as shown in the figure as above.
  • We need one hibernate.cfg.xml file for hibernate configuration which handles connection pulling and other stuff like driver name, username of the database, password of the database, and the following properties..


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

  <property name="hibernate.connection.driver_class">

  <property name="hibernate.connection.url">

  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password"></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="">update</property>
  <!-- Mapping files -->
  <mapping resource="contact.hbm.xml"/>

  • Here the Hibernate tutorial is the database supposed to be created at local host.
  • The dialect property says that we are suing the MySQL database.
  • The show sql property lets us see the comments at the console which ultimately increases readability.
  • Every Class file having the attributes have a classname.hbm.xml file which is the conf file for mapping between POJO and the database. For example we have "contact.hbm.xml" file for contact class.
  • Hibernate accesses all the data through POJO objects and then interacts with the database. For interacting with the database it uses the HQL(Hibernate Query Language)
  • Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column.

Code for

  * Java Class to map to the datbase Contact Table
public class Contact {
  private String firstName;
  private String lastName;
  private String email;
  private long id;

 @return Email
  public String getEmail() {
  return email;

 @return First Name
  public String getFirstName() {
  return firstName;

 @return Last name
  public String getLastName() {
  return lastName;

 @param string Sets the Email
  public void setEmail(String string) {
  email = string;

 @param string Sets the First Name
  public void setFirstName(String string) {
  firstName = string;

 @param string sets the Last Name
  public void setLastName(String string) {
  lastName = string;

 @return ID Returns ID
  public long getId() {
  return id;

 @param l Sets the ID
  public void setId(long l) {
  id = l;

Mapping the Contact class with the Contact Table.

Code for Contact.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

  <class name="roseindia.tutorial.hibernate.Contact" table="CONTACT">
   <id name="id" type="long" column="ID" >
   <generator class="assigned"/>

  <property name="firstName">
   <column name="FIRSTNAME" />
  <property name="lastName">
  <column name="LASTNAME"/>
  <property name="email">
  <column name="EMAIL"/>




Then we need to create a hibernatetutorial database using MySQL running at localhost.

For updating or inserting the data in the database we first need to create the session of hibernate. For that we need to use hibernate session factory which returns the session for hibernate.

The hibernate-session is the main runtime interface between java and Hibernate.

The most important method for this is;.

The code for

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


 * Hibernate example to inset data into Contact table
public class FirstExample {
  public static void main(String[] args) {
  Session session = null;

  // This step will read hibernate.cfg.xml 

and prepare hibernate for use
  SessionFactory sessionFactory = new 

 session =sessionFactory.openSession();
  //Create new instance of Contact and set 

values in it by reading them from form object
 System.out.println("Inserting Record");
  Contact contact = new Contact();
  }catch(Exception e){
  // Actual contact insertion will happen at this step


No comments:

Post a Comment