1. USING ANNOTATION
We mentioned earlier that object to database mapping information may be specified in two ways: (i) using XML files (which we have used so far) and (ii) using annotations. One minor problem of the former method is that we need to make a separate XML file for this mapping. Annotations avoid this problem by allowing developers to specify mapping directly into the POJO Java source file. Consequently, it also develops to readily understand the object to table correspondence during class file development.
2. ENVIRONMENT SETUP FOR HIBERNATE ANNOTATION
The annotations used in hibernate are provided by Oracle (formerly Sun) under javax.persistence package and hibernate has provided implementations for annotations given in that package. So, it is necessary to download the distribution package javax.persistence. We downloaded the .zip file javax.persistence.jar.zip from http://www.java2s.eom/Code/Jar/j/Downloadjavaxpersistencejar. htm and placed the javax.persistence.jar (contained in the .zip file) file in the hibernate_home\ hibernate-release-4.3.1.Final\lib\required directory. Alternatively, place this Jar file in your CLASSPATH or specify it at compile time and runtime using -cp or -classpath option. Note that any other JAR file containing javax.persistence package may also be used.
3. BOOK APPLICATION USING ANNOTATION
Let us use our Book application to understand annotations. Change Book.java as described in the following:
Import annotation interfaces from javax.persistence package as follows:
import javax.persistence.*;
The complete source of the modified annotated Book.java is shown below:
//Book.java
import javax.persistence.*;
@Entity
@Table(name = “tbl_book”)
public class Book {
private int b_id; private int b_price; private String b_title;
public Book(){} public Book(String t, int p) {
this.b_title = t;
this.b_price = p;
}
@Id @GeneratedValue
@Column(name = “no”)
id; }
public int getId() { return b_id; } public void setId(int id ) { this.b_id =
@Column(name = “name”)
public String getTitle() { return b_title; }
public void setTitle(String t ) { this.b_title = t; }
@Column(name = “pr”)
public int getPrice() { return b_price; }
public void setPrice(int p ){ this.b_price = p; }
}
The XML mapping file Book.hbm.xml is no longer needed. So, delete the following line from
SaveBook.java:
cfg.addResource(“Book.hbm.xml”);
Add annotation class to the hibernate Configuration object:
cfg.addAnnotatedClass(Book.class);
The position of @Id annotation determines how Hibernate should access properties on objects. If it is placed on any field, Hibernate accesses object properties directly through fields. In this case, if a field name is different from a column name, @Column annotation must be present for that field and must provide association.
On the other hand, if @Id annotation is placed on any method, object properties are accessed through get and set methods. So, get and set methods must be present. In that case if property name corresponding to a get method is different from column name, @Column annotation must be present for that get method and must provide association.
In our previous example, @Id annotation is placed on the getId() method. Hence, Hibernate uses get and set methods to access object properties. The following annotation instructs Hibernate to access fields of Book object directly:
//Book.java
import javax.persistence.*;
@Entity
@Table(name = “tbl_book”)
public class Book {
@Id @GeneratedValue @Column(name = “no”)
private int b_id;
@Column(name = “pr”)
private int b_price;
@Column(name = “name”)
private String b_title; public Book(){} public Book(String t, int p) {
this.b_title = t;
this.b_price = p;
}
}
Since, Hibernate accesses fields directly, get and set methods are not necessary for Hibernate. However, if an application wants to access those private fields, suitable methods are necessary.
4. FUNCTION OF DIFFERENT ANNOTATIONS
@Entity
It is used to declare a class as an Entity bean. The class should have at least a package scoped no-argument constructor.
@Table
This optional annotation is used to specify the table to store persistent objects. The value of name attribute refers to the table name. If none are used, Hibernate uses the class name as the table name.
@Id
It is used to specify the identifier property of the entity bean.
@GeneratedValue
This optional annotation is used to specify the primary key generation strategy to use. If none are used, default AUTO will be used.
@Column
This optional annotation is used to map a field or a property to a table column. If none are specified, by default, the property name will be used as the column name.
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.