0 votes
in JAVA by
Name Hibernate Annotations?

1 Answer

0 votes
by
Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code, this helps the user to understand the table structure and POJO simultaneously during the development.

If you going to make your application portable to other EJB 3 compliant ORM applications, you must use annotations to represent the mapping information, but still if you want greater flexibility, then you should go with XML-based mappings.

Environment Setup for Hibernate Annotation

First of all you would have to make sure that you are using JDK 5.0 otherwise you need to upgrade your JDK to JDK 5.0 to take advantage of the native support for annotations.

Second, you will need to install the Hibernate 3.x annotations distribution package, available from the sourceforge: (Download Hibernate Annotation) and copy hibernate-annotations.jar, lib/hibernate-comons-annotations.jar and lib/ejb3-persistence.jar from the Hibernate Annotations distribution to your CLASSPATH.

AD

Annotated Class Example

As I mentioned above while working with Hibernate Annotation, all the metadata is clubbed into the POJO java file along with the code, this helps the user to understand the table structure and POJO simultaneously during the development.

Consider we are going to use the following EMPLOYEE table to store our objects −

create table EMPLOYEE (

   id INT NOT NULL auto_increment,

   first_name VARCHAR(20) default NULL,

   last_name  VARCHAR(20) default NULL,

   salary     INT  default NULL,

   PRIMARY KEY (id)

);

Following is the mapping of Employee class with annotations to map objects with the defined EMPLOYEE table −

import javax.persistence.*;

@Entity

@Table(name = "EMPLOYEE")

public class Employee {

   @Id @GeneratedValue

   @Column(name = "id")

   private int id;

   @Column(name = "first_name")

   private String firstName;

   @Column(name = "last_name")

   private String lastName;

   @Column(name = "salary")

   private int salary;  

   public Employee() {}

   

   public int getId() {

      return id;

   }

   

   public void setId( int id ) {

      this.id = id;

   }

   

   public String getFirstName() {

      return firstName;

   }

   

   public void setFirstName( String first_name ) {

      this.firstName = first_name;

   }

   

   public String getLastName() {

      return lastName;

   }

   

   public void setLastName( String last_name ) {

      this.lastName = last_name;

   }

   

   public int getSalary() {

      return salary;

   }

   

   public void setSalary( int salary ) {

      this.salary = salary;

   }

}

Hibernate detects that the @Id annotation is on a field and assumes that it should access properties of an object directly through fields at runtime. If you placed the @Id annotation on the getId() method, you would enable access to properties through getter and setter methods by default. Hence, all other annotations are also placed on either fields or getter methods, following the selected strategy.

Following section will explain the annotations used in the above class.

@Entity Annotation

The EJB 3 standard annotations are contained in the javax.persistence package, so we import this package as the first step. Second, we used the @Entity annotation to the Employee class, which marks this class as an entity bean, so it must have a no-argument constructor that is visible with at least protected scope.
...