On Java Development

All things related to Java development, from the perspective of a caveman.

Hibernate and JPA Annotations

without comments


Early releases of Hibernate used an XML document to instruct Hibernate how to map the Entity class to its related database file. It is the mechanism used to associate properties of the entity class to the file’s fields and is known as object/relational mapping. These days, annotations are the preferred method, but there are some things developers need to watch for.

The Entity
A simple definition of an entity is a class that holds properties and their associated values each of which correspond to a file’s fields and values. From an RPG perspective, think of the entity as a named data structure used to hold the field values of a file’s record. In an RPG program every record read from the file fills the data structure’s fields with values of that record. The RPG program could even account for multiple records using a multiple occurring data structure. The role of the entity within a Java program is very much the same. Through instantiation, more than one copy of the entity can be made and then held in a named array.

It is customary to give the entity class the same name as the physical file. Shown below is an entity class for a file called Customer. For brevity it has only a few properties (DS fields). Also, note that the comment “// Setters and Getters” denotes where the so-called setter and getter methods are placed. These methods provide the means of setting and retrieving the values for each of the defined properties i.e. countryCode, idCardNo, etc, etc. These methods are the only way to set and change those values as each property has been declared as private.

The class that holds the key fields are shown below.

The Object/Relational Mapping File
As mentioned, earlier releases of Hibernate used an XML to create the mapping definitions for each file being used. Shown below is the XML file entry used to map the Customer file.

The definition refers to the class name of the entity and the name of the associated table (case matters). It defines what is known as a composite key that is comprised of the fields for countryCode and idCardNo. The balance of the definition describes each of the remaining fields. That’s it. The table with a composite key has been mapped to Hibernate using XML. Other types are of the header/detail variety and are a bit more involved.
Mapping using Hibernate Annotations
Using Hibernate annotations does the same work as the XML mapping and allows Hibernate to place the record values into the entity’s properties using the so-called setter and getter methods. Shown below is the entity class with the proper Hibernate annotations.

As can be seen, it is much simpler to use annotations.

List of Hibernate Annotations
This link lists all of Hibernate’s annotations for version 3.5.

This link lists all of Hibernate’s annotations for version 4. Some of the annotations of 3.5 have been deprecated. For example, Entity is available in 3.5 but it has been earmarked to be replaced (deprecated) in future versions and recommends DynamicUpdate. This link presents the annotations as of 4.2. The annotations provided by Hibernate are ready to use out of the box with no extra configuration required.

JPA Annotations
Annotations of the Java Persistence API can be seen here. To use them properly the project must be configured to use JPA.

Which One?
To get some kind of relevancy, it’s often helpful to see what the industry is doing. The chart below offers a bit of insight about which annotation method is most often used.

A Final Caveat
The Java Persistence API is part of the Java Extensions packages. This means that it will be easy to include JPA annotations into your class files if not careful to notice from which package the annotation originate. If the annotations are from the javax.persistence package they are of the JPA variety. However, if the annotations are from the org.hibernate.annotations package, they are from Hibernate. It is not a good idea to mix them as doing so obfuscates the design. Was the project started with JPA or not? What was the original intent? This post illustrates the problems which can be encountered when developers try mixing the annotations.

Written by admin

January 12th, 2014 at 4:07 pm

Leave a Reply

You must be logged in to post a comment.