/* ************ COMPLETE OF GOOGLE JQUERY ************ */ HIBERNATE 4U

Wednesday, July 7, 2010

Hiber nate Tools Introduction:


In this tutorial you will see how to persist the java objects using the Hibernate Object/Relational Mapping (ORM) framework. Hibernate automates ORM and considerably reduces the number of lines of code needed to persist the object in the database. This example demonstrates how to automatically generate code from the object/relational mapping file, thus saving the developers time. This helps the developers to focus on the business problem rather than doing repetitive coding work.
Hibernate uses XML document or the properties file to define the object/relational mapping. The object/relational mapping file contains the mapping between the Java object and the corresponding database table. This example illustrates how to create the ORM using the XML document.
First let's setup the environment. I am using
  • Eclipse IDE 3.4
  • Hibernate Core 3.3
  • Hibernate Tools 3.2
  • HSQLDB 1.8
You can download the Hibernate Core and Hibernate Tools here. HyperSQL DataBase is a 100% lightweight Java SQL Database Engine. You can download HSQLDB from this site http://hsqldb.org/ .
To install the Hibernate Tools, extract the HibernateTools-3.X.zip file and move all the files inside the features folder into the features folder of the eclipse installation directory and move all the files inside the plugins folder into the plugins folder of the ecilpse installation directory. Restart the eclipse.
First create a new Java project. Add the following lib files on the java build path.
01.antlr-2.7.6
02.commons-collections-3.1
03.dom4j-1.6.1
04.hibernate3
05.hsqldb
06.javassist-3.4.GA
07.jta-1.1
08.slf4j-api-1.5.6
09.slf4j-simple-1.5.6
The hibernate3.jar contains all the core hibernate files. The hsqldb.jar is used, to connect with the HSQL database, if you are using someother database then you need to include that jar file instead of this. The slf4j-api-1.5.6 jar file is used for logging the informations, you can also use other alternatives like log4j, to do that include that jar file instead of slf4j-simple-1.5.6 jar file to your classpath.
Once you have installed the hibernate tools, you will have the option to change to Hibernate prespective. Go to Window -> Open Prespective -> Other, the following dialog box appears, select Hibernate and click the Ok button.

Now let's see how to define the object/relational mapping using the XML document. This document has hbm.xml extension. We will now create the object/relational mapping for the simple class that holds course related details. To do that create a package com.vaannila.course in the src directory. The Hibernate XML mapping file will be created in this directory. To create the mapping file, switch to Hibernate prespective, right click the project folder and select New -> Hibernate XML Mapping File(hbm.xml)

In the pop up window select the course folder, enter the file name as Course.hbm.xml and click Next. In the next window click Finish.

Add the following code to the Course.hbm.xml file.
01.xml version="1.0"?>
02.
03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04."http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05.<hibernate-mapping>
06.
07. <class name="com.vaannila.course.Course" table="COURSES">
08. <meta attribute="class-description">
09. This class contains the course details.
10. meta>
11. <id name="courseId" type="long" column="COURSE_ID">
12. <generator class="native"/>
13. id>
14. <property name="courseName" type="string" column="COURSE_NAME" not-null="true" />
15. class>
16.
17.hibernate-mapping>
The hibernate-mapping element is the root element. The class element is used to map the Java class with the database table. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute of the class element. The meta element is used to create the class description. The id element is used to create the primary key. The name attribute of the id element refers to the property in the Course class and the column attribute refers to the column in the COURSES table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type and vice versa. The generator element within the id element is used to automatically generate the primary key values. When the class attribute of the generator element is set to native, hibernate picks either identity, sequence or hilo algorithm depending upon the capabilities of the underlying database. The property element is used to link a property in the Java class to a column in the database table.
The next step is to create the Hibernate configuration file. On startup, Hibernate looks for a file called hibernate.cfg.xml in the root of the classpath. To create the Hibernate Configuration File, right click the project, select New -> Hibernate Configuration File (cfg.xml)

By default the file name will be hibernate.cfg.xml, select the src directory and click Next.

Select the database dialect as HSQL. This property indicates the particular SQL variant that Hibernate generates. Select the "org.hsqldb.jdbcDriver" option for the driver class. Enter the connection url "jdbc:hsqldb:hsql://localhost". Enter the user name as "sa" and click Finish. These properties specifies the necessary configuration for the JDBC connection.

The show_sql option, if set to true will display all the executed SQL queries on the console.
The property hbm2ddl.auto, if set to create, will drop and re-create the database schema on startup.
In the end we add the Course.hbm.xml file to the configuration. The implementation of the hibernate.cfg.xml file is shown below.
01.xml version="1.0" encoding="UTF-8"?>
02.
03."-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04."http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd">
05.<hibernate-configuration>
06. <session-factory>
07. <property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriverproperty>
08. <property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhostproperty>
09. <property name="hibernate.connection.username">saproperty>
10. <property name="connection.password">property>
11. <property name="connection.pool_size">1property>
12. <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialectproperty>
13. <property name="show_sql">trueproperty>
14. <property name="hbm2ddl.auto">createproperty>
15. <mapping resource="com/vaannila/course/Course.hbm.xml"/>
16. session-factory>
17.hibernate-configuration>
Once the Hibernate configuration file is created we need to create a Hibernate console configuration. To do this right click the project folder, select New -> Hibernate Console Configuration.

The Hibernate Console configuration wizard appears.

By default the wizard will load the Hibernate configuration file information. Just click the Finish button to create the Hibernate console configuration.
Once the Hibernate console configuration is created, you can generate code by selecting the Hibernate Code Generation Configurations option form the toolbar.

The Hibernate Code Generation wizard will be displayed. Now select the output directory as the src directory.

In the Exporters tag select the "Use Java 5 Syntax" option and "Domain Code(.java)" option.

In the Refresh tab select the options as shown below and click the Run button to generate the code.

The following Course.java class will be generated from the Course.hbm.xml mapping file.
01.package com.vaannila.course;
02.
03.// Generated May 30, 2009 6:49:31 AM by Hibernate Tools 3.2.4.GA
04.
05./**
06.* This class contains the course details.
07.*
08.*/
09.public class Course implements java.io.Serializable {
10.
11. private long courseId;
12. private String courseName;
13.
14. public Course() {
15. }
16.
17. public Course(String courseName) {
18. this.courseName = courseName;
19. }
20.
21. public long getCourseId() {
22. return this.courseId;
23. }
24.
25. public void setCourseId(long courseId) {
26. this.courseId = courseId;
27. }
28.
29. public String getCourseName() {
30. return this.courseName;
31. }
32.
33. public void setCourseName(String courseName) {
34. this.courseName = courseName;
35. }
36.
37.}
Now create the HibernateUtil class. The HibernateUtil class helps in creating the SessionFactory from the Hibernate configuration file. The SessionFactory is threadsafe, so it is not necessary to obtain one for each thread. Here the static singleton pattern is used to instantiate the SessionFactory. The implementation of the HibernateUtil class is shown below.
01.package com.vaannila.util;
02.
03.import org.hibernate.SessionFactory;
04.import org.hibernate.cfg.Configuration;
05.
06.public class HibernateUtil {
07. private static final SessionFactory sessionFactory;
08. static {
09. try {
10. sessionFactory = new Configuration().configure()
11. .buildSessionFactory();
12. } catch (Throwable ex) {
13. System.err.println("Initial SessionFactory creation failed." + ex);
14. throw new ExceptionInInitializerError(ex);
15. }
16. }
17.
18. public static SessionFactory getSessionFactory() {
19. return sessionFactory;
20. }
21.}
 
Now we will create a class with the main() method to run the application.
001.package com.vaannila.course;
002.
003.import java.util.List;
004.import java.util.Iterator;
005.
006.import org.hibernate.HibernateException;
007.import org.hibernate.Session;
008.import org.hibernate.Transaction;
009.
010.import com.vaannila.util.HibernateUtil;
011.
012.public class Main {
013.
014. public static void main(String[] args) {
015. Main obj = new Main();
016. Long courseId1 = obj.saveCourse("Physics");
017. Long courseId2 = obj.saveCourse("Chemistry");
018. Long courseId3 = obj.saveCourse("Maths");
019. obj.listCourse();
020. obj.updateCourse(courseId3, "Mathematics");
021. obj.deleteCourse(courseId2);
022. obj.listCourse();
023. }
024.
025. public Long saveCourse(String courseName)
026. {
027. Session session = HibernateUtil.getSessionFactory().openSession();
028. Transaction transaction = null;
029. Long courseId = null;
030. try {
031. transaction = session.beginTransaction();
032. Course course = new Course();
033. course.setCourseName(courseName);
034. courseId = (Long) session.save(course);
035. transaction.commit();
036. } catch (HibernateException e) {
037. transaction.rollback();
038. e.printStackTrace();
039. } finally {
040. session.close();
041. }
042. return courseId;
043. }
044.
045. public void listCourse()
046. {
047. Session session = HibernateUtil.getSessionFactory().openSession();
048. Transaction transaction = null;
049. try {
050. transaction = session.beginTransaction();
051. List courses = session.createQuery("from Course").list();
052. for (Iterator iterator = courses.iterator(); iterator.hasNext();)
053. {
054. Course course = (Course) iterator.next();
055. System.out.println(course.getCourseName());
056. }
057. transaction.commit();
058. } catch (HibernateException e) {
059. transaction.rollback();
060. e.printStackTrace();
061. } finally {
062. session.close();
063. }
064. }
065.
066. public void updateCourse(Long courseId, String courseName)
067. {
068. Session session = HibernateUtil.getSessionFactory().openSession();
069. Transaction transaction = null;
070. try {
071. transaction = session.beginTransaction();
072. Course course = (Course) session.get(Course.class, courseId);
073. course.setCourseName(courseName);
074. transaction.commit();
075. } catch (HibernateException e) {
076. transaction.rollback();
077. e.printStackTrace();
078. } finally {
079. session.close();
080. }
081. }
082.
083. public void deleteCourse(Long courseId)
084. {
085. Session session = HibernateUtil.getSessionFactory().openSession();
086. Transaction transaction = null;
087. try {
088. transaction = session.beginTransaction();
089. Course course = (Course) session.get(Course.class, courseId);
090. session.delete(course);
091. transaction.commit();
092. } catch (HibernateException e) {
093. transaction.rollback();
094. e.printStackTrace();
095. } finally {
096. session.close();
097. }
098. }
099.}
The first call to the openSession() method begins the session. The session.beginTransaction() method is used to start a new transaction. Here we have four different methods to perform the CRUD operations.
The saveCourse() method is used to save a new Course object to the database. In the saveCourse()method a new object of the Course class is created and the courseName value is set, the courseId value is auto generated so the value needn't be set here. The session.save() method is used to persist the value in the database and once the value is saved, the id value (Primary key) is returned. Here the courseId value is of type long so we typecast the returned value to Long. Once the object is saved, the transaction is committed. If any exception occurs then the transaction is rolledback. The transaction ends either through commit or rollback action. Once the transaction ends the session is closed.
The listCourse() method is used to list all the courses. The session.createQuery() method is used to create a query object which helps in retrieving the persistant objects. Here we use Hibernate Query Language (HQL). "from Course" returns a list of all the courses in the COURSES table. Note that in the HQL we only specify the java class names and not the table names. Later, using the for loop we iterate the list of courses and display them on the console.
The updateCourse() method is used to update the course name. It takes the courseId and the new courseName as input parameters. The courseId is used to retrive the actual course object using the session.get() method. The session.get() method takes the class name and the id value as the input parameter and returns the corresponding Object. Here we set the new course name to the Course object and commit the transaction. We need not explicitly call the session.save() method to persist the object. Hibernate will automatically update the database when the state of the persistant object is modified inside a transaction. This feature of Hibernate is called automatic dirty checking.
The deleteCourse() method is used to delete a Course object. This method is similar to the updateCourse() method, here instead of updating the object we call the session.delete() method to delete a persistant object.
The figure below shows the final directory structure of the example.

To run the example, first start the HSQLDB server. To start the HSQLDB server, run the command prompt, go to the hsqldb directory and execute the following command.
1.java -cp ./lib/hsqldb.jar org.hsqldb.Server

After starting the server, run the Main class. On startup the database schema will be created and the following actions will happen.
01.public static void main(String[] args) {
02. Main obj = new Main();
03. Long courseId1 = obj.saveCourse("Physics");
04. Long courseId2 = obj.saveCourse("Chemistry");
05. Long courseId3 = obj.saveCourse("Maths");
06. obj.listCourse();
07. obj.updateCourse(courseId3, "Mathematics");
08. obj.deleteCourse(courseId2);
09. obj.listCourse();
10.}
Each time on startup the database schema will be droped and recreated, if you want to use the existing one change the value of hbm2ddl.auto option to update.
The SQL statement generated gets displayed on the console. This is set using the show_sql option in the hibernate configuration file.
01.Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
02.Hibernate: call identity()
03.Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
04.Hibernate: call identity()
05.Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
06.Hibernate: call identity()
07.Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
08.Physics
09.Chemistry
10.Maths
11.Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
12.Hibernate: update COURSES set COURSE_NAME=? where COURSE_ID=?
13.Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
14.Hibernate: delete from COURSES where COURSE_ID=?
15.Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
16.Physics
17.Mathematics
Now let's check whether the database schema is created and the data is inserted into the COURSES table or not. Open a new command prompt, go to the hsqldb installed directory and type the following command.
1.java -cp ./lib/hsqldb.jar org.hsqldb.util.DatabaseManager

The following dialog box pops up. Select the Type as "HSQL Database Engine Server" and click Ok.

The HSQL Databse Manager window opens. Here you can enter the following SQL statement, and see the data is successfully stored in the COURSES table.

To shutdown the HSQLDB properly enter "shutdown" and click the Execute button. Alternatively you can also shutdown programmatically by closing the SessionFactory, using HibernateUtil.getSessionFactory().close() method.

Total Pageviews

ChandraSekhar  © 2011

Back to TOP