Wednesday, 21 February 2018

Hibernate - JPA Annotations with explanation

             In this post, we can learn some important Hibernate - JPA Annotations.  The following are the main Annotations which are used in the Hibernate.  For Spring annotations, Refer this Spring Annotations


 1) @Entity

     Annotate all your entity beans with @Entity. This contains in the javax.persistence package. 


@Entity
public class Employee implements Serializable {
  //properties with setters and getters
}



2) @Table

      The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database.

       The @Table annotation provides four attributes, allowing you to override the name of the table,  its catalog,  and its schema, and enforce unique constraints on columns in the table.


@Entity
@Table(name = "employee")
public class Employee implements Serializable {
     //properties with setters and getters
}


3) @Column

        The @Column annotation is used to specify the details of the column to which a field or property will be mapped.

      You can use column annotation with the following most commonly used attributes,
  • name - attribute permits the name of the column to be explicitly specified.
  • length - attribute permits the size of the column used to map a value particularly for a String value
  • nullable - attribute permits the column to be marked NOT NULL when the schema is generated
  • unique - attribute permits the column to be marked as containing only unique values.


@Entity
@Table(name = "employee")
public class Employee implements Serializable {
 
     @Column(name = "emp_name")
     private String name;
   
...
}


4) @Id

             This annotation specifies that a field is mapped to a primary key column in the table.
Since the column emp_id is a primary key,  we have to use this annotation as follows,

@Entity
@Table(name = "employee")
public class Employee implements Serializable {
 
     @Id
     @Column(name = "emp_id")
     private int empId;
   
...
}


5) @GeneratedValue

           If the values of the primary column are auto-increment, we need to use this annotation to tell Hibernate knows, along with one of the following strategy types: AUTO, IDENTITY, SEQUENCE, and TABLE. 
           In below example, strategy AUTO implies that the generated values are unique at database level.


@Entity
@Table(name = "employee")
public class Employee implements Serializable {
 
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "emp_id", updatable = false, nullable = false)
   
...
}



6)  @OrderBy


   The @OrderBy orders the column values and put into the list as ordered list data. By Default @OrderBy orders the element in ascending order. We need to define property name on the basis of which values will be ordered.



@Entity
@Table(name = "employee")
public class Employee implements Serializable {
 
       @OneToMany(cascade=CascadeType.ALL)
       @JoinColumn(name="address_id")
       @OrderBy("name")
       private List<Address> address;
   
...
}


Hibernate Association Mapping Annotations

7)  @OneToOne

      The @OneToOne annotation is using for mapping between two tables that should be One To One mapping. 
      The below example illustrates the OneToOne Mapping between Employee and Department Entity.

Employee.java
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
   
     @Id
     @Column(name = "emp_id")
     @GeneratedValue
     private int id;
   
     @Column(name="emp_name")
     private String name;

     @OneToOne(cascade=CascadeType.ALL)  
     @JoinColumn(name="dept_id")
     private Department department;
   
     // setters and getters
}
 

Department.java


@Entity
@Table(name = "department")
public class Department implements Serializable {
   
     @Id
     @Column(name = "dept_id")
     @GeneratedValue
     private int id;
   
     @Column(name="dept_name")
     private String name;

     @OneToOne(mappedBy="department")  
     private Employee employee;
   
     // setters and getters
}
 


8) @ManyToOne

    The below example illustrates the  ManyToOne Mapping between Employee and Address Entity.  One Employee can have multiple addresses.

Address.java
@Entity
@Table(name = "address")
public class Address implements Serializable {
   
     @Id
     @Column(name = "address_id")
     @GeneratedValue
     private int id;
   
     @Column(name="add_type")
     private String type;

     @ManyToOne
     @JoinColumn(name="emp_id") 
     private Employee employee;
   
     // setters and getters
}
 

9) @OneToMany

     It's opposite to @ManyToOne. The previous example, One employee can have multiple address so mapping should be OneToMany as below.

Employee.java
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
   
     @Id
     @Column(name = "emp_id")
     @GeneratedValue
     private int id;
   
     @Column(name="emp_name")
     private String name;

     @OneToMany(mappedBy="employee") 
     private Set<Address> address;
   
     // setters and getters
}
 


10) @ManyToMany

It's example of Student and Department association.  Many students mapped with many departments using std_id and dept_id.

Student.java
@Entity
@Table(name = "student")
public class Student {
    
     @Id 
     @GeneratedValue
          (strategy = GenerationType.IDENTITY)
     @Column(name="std_id")
     private int id;

     @Column(name"std_name")
     private String name;

     @ManyToMany 
     @JoinTable(name="student_dept",  
      joinColumns = @JoinColumn(name="std_id"),
      inverseJoinColumns = @JoinColumn(name="dept_id"))  
     private Collection<Department> departments;

    // Setters and getters
    ....
}

Department.java

@Entity
@Table (name="student_dept")
public class Department {
     
     @Id 
     @GeneratedValue (strategy=GenerationType.IDENTITY)
     @Column(name="dept_id")
     private int id;
     
     @Column(name="dept_name")
     private String name;

     @ManyToMany(mappedBy="departments")
     private Collection<Student> students;

     // Setters and getters
     ...
}









Related Post:
1) Advantages of Hibernate over JDBC
2) What are the Core Interfaces of Hibernate framework ?
3) Spring MVC with Hibernate CRUD Example


1 comment:

  1. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iphone app training course in bangalore
    mobile app training institutes bangalore

    ReplyDelete