Kick starting Hibernate

I had worked on Hibernate some years back. I decided it was time for me to do a refresher on hibernate and check out its new features and also verify if I remember anything that had learned few years back.

Since I was only interested in checking out hibernate’s OR capabilities, I limited myself to accumulating the following tools.

  1. Eclipse 3.3 (Decided to check out Europa 🙂 )
  2. MySQL
  3. Hibernate
  4. Execute Query (Nice IDE for Database connectivity)

This post and subsequent posts will be a walk thru of different aspects of hibernate’s capabilities.

I created a Java project in eclipse. Created a library folder and added the following mandatory jars:

  1. antlr.jar
  2. asm.jar
  3. asm-attrs.jar
  4. cglib.jar
  5. commons-collections.jar
  6. commons-logging.jar
  7. dom4j.jar
  8. hibernate.jar
  9. jta.jar
  10. log4j.jar
  11. mysql-connector-java-5.0.6-bin.jar (for MySQL)

Pick the appropriate versions available within the hibernate distribution. Next get the hibernate.cfg.xml setup. Take the default cfg.xml available in the hibernate distribution.

Here’s the config of my cfg.xml file



      <!-- Database connection settings -->
      <property name="connection.driver_class">
      <property name="connection.url">
      <property name="connection.username">Your username</property>
      <property name="connection.password">Your Password</property>

      <!-- JDBC connection pool (use the built-in) -->
      <property name="connection.pool_size">1</property>

      <!-- SQL dialect -->
      <property name="dialect">

      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>

      <!-- Disable the second-level cache  -->
      <property name="cache.provider_class">

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

      <!-- Drop and re-create the database schema on startup -->
      <property name="">update</property>

      <!-- Your custom POJO persistent classes need to be added here. -->
      <mapping resource="com/tutorial/hibernate/simple/Person.hbm.xml"/>



The attribute automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. The valid values for the attribute are:

validate | update | create | create-drop.

Now that the basic setup for hibernate is complete. Let’s move on. First create a user in MySQL database using the following scripts:

CREATE USER <username>@’localhost’;

GRANT ALL PRIVILEGES ON <database>.* TO <username>@’localhost’ IDENTIFIED BY <password> WITH GRANT OPTION;


Next create Person table is the database. The create table script is as below:

id VARCHAR(128),
firstname VARCHAR(80),
lastname VARCHAR(80),
dob timestamp)

Next create a java class named Person with the following private attributes:

id, firstName, lastName, dob.

Create the mapping xml file named Person.hbm.xml. The contents of the XML are as below:

   <class name="com.tutorial.hibernate.simple.Person" table="PERSON">
      <id name="id">
         <generator class="assigned"/>
      <property name="firstName"/>
      <property name="lastName"/>
      <property name="dob" type="timestamp" />
      <property name="title"/>

The following is a program to insert a row in the Person table.

        Configuration cfg = new Configuration();
        SessionFactory sessFactory = cfg.buildSessionFactory();
        Session session = sessFactory.openSession();
        Person p = new Person();
        p.setDob(new java.util.Date());

Now let’s try some variations. Change the type attribute in the Person.hbm.xml from timestamp to date. You will notice that the hh:mm:ss information will be lost due to this change. A developer should take note of this fact.


2 thoughts on “Kick starting Hibernate

  1. Hi,
    I dint get you name from your blog. But I feel what you have put up is great. I will like to constantly view this site… !! keep posting and thanks…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s