Hibernate configuration and mapping file

There are several ways to configure Hibernate. Having the HibernateUtil convenience class ready as we had learnt earlier in the previous post, we can go ahead using it to configure the Hibernate application with the Oracle Database.

Note the following line in the HibernateUtil class,

This would search for the Hibernate XML file for the configuration data.

Now if we remove the configure() call from the above snippet as –

Now the application would rather look up for the hibernate.properties file for the configuration data. This is an important point to note.

So let’s check out how the hibernate configuration XML file, named as hibernate.cfg.xml would look like:

Above are the most common properties that need to be assigned before persisting the data into the database in a Hibernate application.

Also in this article, I would like to provide the basic structure of a Hibernate mapping file.

Let’s consider an Employee entity bean as:

The corresponding mapping file would look something like:

<hibernate-mapping> is always the root element in a mapping file.

<generator> element is used to automatically generate the primary key values. There are different possible values for the generator class, such as native, assigned, foreign, etc., which I will be comparing some time in a later posts.

Also, every mapped class must have a primary key column of the database table.

There is something called the Object Relational Mapping done over these individual Hibernate mapping files, to create relationships among the respective database tables. These relationships can be expressed as:

  1. Many-to-One
  2. One-to-One
  3. One-to-Many
  4. Many-to-Many

I would try explaining each of these in separate posts. So far, you are most welcome to post your queries as comments to this post.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.