The chasm between the Object Oriented approach followed by almost all the language or platforms on which applications are developed and the language used by database servers (SQL) is considerably big. This divide exists because the application development languages/platforms deals with objects, attributes and classes whereas SQL deals with rows, columns and tables. There are many frameworks that provide the means to bridge this gap. These frameworks do so by mapping the database artefacts to their object-oriented counterparts. Such frameworks are called Object Relational Mapping or ORM frameworks. In the world of Java, Hibernate is one of the widely used ORM frameworks. In its 2.x version, Hibernate depended on XML files to map Java artefacts to Database artefacts. However, in 3.0 and above, Hibernate provides annotations as an alternative to the XML based mapping. Use of annotations in Hibernate will be the focus of this discussion. The first section will be about the whys and wherefores of annotations in Hibernate. In the second section, the focus will be on the steps required to use the annotations for relational-object mapping. The third section will be about a real world example that makes use of Hibernate 3 and annotations.
Annotations in Hibernate – the Whys and Wherefores
To understand annotation used in Hibernate, two aspects need to be understood. They are
1. Annotations in Java
2. Annotations in Hibernate
Between these two aspects, former of the deals with annotations in general and later deals with annotations specific to Hibernate. Here are the details.
1. Annotations in Java
Before we move onto how Hibernate makes use of annotations, let us take a bird’s eye view of what
annotations are. By definition Annotations are “is a special form of syntactic metadata that can be added to
Java source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc
tags, Java annotations can be reflective in that they can be embedded in class files generated by the
compiler and may be retained by the Java VM to be made retrievable at run-time”. From the definition it is
clear that Annotations in Java have two components. They are:
a. The Metadata
It is of the form “@” where is the name of the metadata. Annotations are metadata of
this form. They can be used to decorate classes, methods, variables, parameters and packages. For
example, the following statements tell the metadata processor that the method is overriding the method
of the base class
@override
Public void display(String name)
{
System.out.println(“Hello “+ name);
}
One important point to keep in mind is that annotations are not discarded after compilation to byte code.
By applying certain policies, they can be retained during runtime. This is not the case with Javadoc tags.
Once compiled, they are lost. In other words, the byte code does not contain the Javadoc tags.
b. The Metadata Processor
It comes into picture during compilation. Metadata Processors or Annotation Processors, as they are
commonly called, are plug-ins to the compiler. A Processor can do a variety of things for the annotated
code including generation of source code files. One important point about Processors is that cannot do
any modification to the annotated code.
That completes the bird’s eye view of Annotations. Let us move onto Annotations in Hibernate.
2. Annotations in Hibernate
Any ORM does four basic mapping. They are:
a. Table mapping
b. Column mapping
c. Key mapping
d. Relationship mapping
How each framework performs the mapping is what makes them different or unique, so is how each
framework wants mapping to be described. Hibernate allows the mapping to be described using either XML
file or Annotations. These two options cannot be mixed and matched. Let us see how Hibernate uses
Annotations for describing the mappings. However, among the four mappings mentioned above, Relationship
mapping will not be a part of this discussion as it is out scope.
a. Table mapping
To map a table, @Table is used. It takes the name of the table that is passed using the name property of the
@Table annotation. For example, the following statements map a table named ‘Products’ to a class named
Product
@Table (name=”Products”)
public class Products {
//…other declarations
}
b. Column mapping
The annotation used to map a column of a table to an attribute of a class is @Column. The name of the
column of the table to be mapped is passed to the annotation using the name property of the @Column.
Following statements map ‘Product_name’ column of ‘Products’ table to ‘name’ field of ‘Products’ class.
@Table (name=”Products”)
public class Products {
//…other declarations
@Column (name=”Product_name”)
String name;
}
c. Key mapping:
Key, in this context, means Primary key of the table being mapped. The @Id annotation is used to map a
field of a class to the Primary key column of the table. @Id is used along with @Column. More specifically,
@Id is used for a field that is already decorated with @Column. The reason is that unless a field is mapped to
a column, the field cannot be considered for Primary key mapping. For example, the following statements
map a field named ‘productId’ to the primary key of the table ‘Products’
@Table (name=”Products”)
public class Products {
//…other declarations
@Id
@Column (name=”Product_id”)
long productId;
}
That completes the whys and wherefores of Annotations in general and how annotations are used in
Hibernate. Let’s move on to the steps for using annotations for mapping.
