HIBERNATE An Introduction

Arun Srec

What is Hibernate? It is an object-relational mapping (ORM) solution for Java  We make our data persistent by storing it in a database  Hibernate takes care of this for us  .

 .  Hibernate is used convert object data in JAVA to relational database tables.Object-Relational Mapping It is a programming technique for converting object-type data of an object oriented programming language into database tables.

   Makes an application portable to all SQL databases. Hibernate provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC. .Why Hibernate and not JDBC?  JDBC maps Java classes to database tables (and from Java data types to SQL data types) Hibernate automatically generates the SQL queries.

”J.Hibernate vs.K.executeUpdate(“INSERT INTO book VALUES(“Harry Potter”.Rowling”)). JDBC (an example)  JDBC tuple insertion – st. Hibernate tuple insertion – session.save(book1).  .

Architecture Hibernate sits between your code and the database Maps persistent objects to tables in the database .

 In this example we use MySQL database. and information about the hosts of these events.Example Application Consider an application that can store events we want to attend.  .

java package events. } public void setTitle(String title) { this. } } private void setId(Long id) { this. public Event() {} public Long getId() { return id. } public void setDate(Date date) { this.The first persistent class – Event. import java.util. public class Event { private Long id. } public String getTitle() { return title.title = title. private Date date. private String title.id = id.*. //unique identifier } .date = date. } public Date getDate() { return date.

Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp“ column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping> By default. Hence. provide a different name. property name  column name.The Mapping file .hbm. .xml <hibernate-mapping> <class name="events. But „date‟ is a reserved keyword in most databases.Event.

url">jdbc:mysql://localhost/event_database</property> <property name="connection.cfg.jdbc.Database connection settings --> <property name="connection.password">root</property> <!-.dialect.Drop and re-create the database schema on startup --> <property name="hbm2ddl.0.dtd"> <hibernate-configuration> <session-factory> <!-.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.Hibernate Configuration file hibernate.driver_class">com.auto">create</property> ……………….0//EN" "http://hibernate.SQL dialect --> <property name="dialect">org.xml"/> </session-factory> </hibernate-configuration> .hibernate.MySQLDialect</property> <!-.xml <?xml version='1.net/hibernate-configuration-3.username">root</property> <property name="connection.hbm.mysql. <mapping resource="events/Event.sourceforge.Driver</property> <property name="connection.

Session) A short-lived object  Representing a conversation between the application and the database  Wraps a JDBC connection  Factory for Transaction . Session (org.hibernate.

 SessionFactory (org.SessionFactory) A cache of compiled mappings for a single database  A factory for Session .hibernate.

*.HibernateUtil.cfg. } } public static SessionFactory getSessionFactory() { return sessionFactory.err. .cfg.configure(). import org.java package util.*.println("Initial SessionFactory creation failed. } } Provides startup and easy use of SessionFactory.hibernate.xml sessionFactory = new Configuration().hibernate. static { try { // Create the SessionFactory from hibernate. throw new ExceptionInInitializerError(ex). import org.buildSessionFactory()." + ex). public class HibernateUtil { private static final SessionFactory sessionFactory. as it might be swallowed System. } catch (Throwable ex) { // Make sure you log the exception.

Date.*.createAndStoreEvent("My Event". new Date()). } HibernateUtil. public class EventManager { public static void main(String[] args) { EventManager mgr = new EventManager().util.Session.java package events.equals("store")) { mgr. } . import util.close(). import java. if (args[0].EventManager.hibernate.getSessionFactory().lang. import org. import java.HibernateUtil.

Date theDate) { Session session = HibernateUtil.commit(). session.getSessionFactory(). theEvent.private void createAndStoreEvent(String title.beginTransaction(). } } . theEvent.getCurrentSession(). session.setDate(theDate). Event theEvent = new Event().setTitle(title).getTransaction().save(theEvent). session.

xml file will tell Ant to add all files in the lib directory ending with .Building with Ant – build. It will also copy all non-Java source files to the target directory.g. configuration and Hibernate mapping files.   .xml  Apache Ant is a software tool for automating software build processes. e. The build.jar to the classpath used for compilation.

Run! >ant run Daction=store .

Sources and References www.hibernate.org  www.jar file here)  .org  www.apache.ant.slf4j.org/dist/ (Find the missing .

Questions .