The largest Interview Solution Library on the web

Hibernate Tutorials/
« Previous | 1 | 2 | 3 | Next »
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 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,

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

import javax.persistence.*;

@Table(name ="EMPLOYEE")
@Column(name ="id")
privateint id;

@Column(name ="first_name")
privateString firstName;

@Column(name ="last_name")
privateString lastName;

@Column(name ="salary") privateint salary;

publicint getId(){
return id;
publicvoid setId(int id ){ = id;
publicString getFirstName(){<
return firstName;
publicvoid setFirstName(String first_name ){
this.firstName = first_name;
publicString getLastName(){
return lastName;
publicvoid setLastName(String last_name ){
this.lastName = last_name;
publicint getSalary(){
return salary;
publicvoid setSalary(int salary ){
this.salary = salary;


Hibernate detects that the @Id annotation is on a field and assumes that it should access properties on 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.
« Previous | 1 | 2 | 3 | Next »

copyright © 2014 - all rights riserved by