Hibernate Dielect example

Hibernate Dielect example

<?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=”hibernate.hbm2ddl.auto”>update</property>
<!– Mapping files –>
<mapping resource=”contact.hbm.xml“/>

In the above configuration file we specified to use the “hibernatetutorial” which is running on localhost and the user of the database is root with no password. The dialect property is org.hibernate.dialect.MySQLDialect which tells the Hibernate that we are using MySQL Database. Hibernate supports many database. With the use of the Hibernate (Object/Relational Mapping and Transparent Object Persistence for Java and SQL Databases), we can use the following databases dialect type property:

  • DB2- org.hibernate.dialect.DB2Dialect
  • HypersonicSQL – org.hibernate.dialect.HSQLDialect
  • Informix – org.hibernate.dialect.InformixDialect
  • Ingres – org.hibernate.dialect.IngresDialect
  • Interbase – org.hibernate.dialect.InterbaseDialect
  • Pointbase – org.hibernate.dialect.PointbaseDialect
  • PostgreSQL – org.hibernate.dialect.PostgreSQLDialect
  • Mckoi SQL – org.hibernate.dialect.MckoiDialect
  • Microsoft SQL Server – org.hibernate.dialect.SQLServerDialect
  • MySQL – org.hibernate.dialect.MySQLDialect
  • Oracle (any version) – org.hibernate.dialect.OracleDialect
  • Oracle 9 – org.hibernate.dialect.Oracle9Dialect
  • Progress – org.hibernate.dialect.ProgressDialect
  • FrontBase – org.hibernate.dialect.FrontbaseDialect
  • SAP DB – org.hibernate.dialect.SAPDBDialect
  • ass=”klink”>Sybaseorg.hibernate.dialect.SybaseDialect
  • Sybase Anywhere – org.hibernate.dialect.SybaseAnywhereDialect

The <mapping resource=”contact.hbm.xml“/> property is the mapping for our contact table.

Writing First Persistence Class
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. Here is the code for Contact.java:

package com.hibernate;

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;

="color:white;"> /**
* @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 Object to the Database Contact table
The file contact.hbm.xml is used to map Contact Object to the Contact
table in the database. Here is the code for contact.hbm.xml:

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

<class name=”example.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”/>

Setting Up MySQL Database
In the configuration file(hibernate.cfg.xml) we have specified to use hibernatetutorial database running on localhost. So, create the databse (“hibernatetutorial”) on the MySQL server running on localhost.

Developing Code to Test Hibernate example
Now we are ready to write a program to insert the data into database. We should first understand about the Hibernate’s Session. Hibernate Session is the main runtime interface between a Java application and Hibernate. First we are required to get the Hibernate Session.SessionFactory allows application to create the Hibernate Sesssion by reading the configuration from hibernate.cfg.xml file. Then the save method on session object is used to save the contact information to the database:


Here is the code of FirstExample.java

package com.hibernate;

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

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