Hibernate Annotations

Hibernate allows using XML based mapping files to map the POJO elements to database. However, we can also use annotations for the same as they are easy to use and make the development process faster.

This article mentions the basic and necessary annotations for creating entity classes.

Hibernate uses and supports the JPA 2 persistence annotations.So, we will use the JPA annotations rather than Hibernate specific ones so that we have the flexibility to deploy the entities to environments using other ORM tools that support JPA 2.
 
hibernate annotations
 

Creating Entity Beans with @Entity

In Hibernate, we can annotate a class with @Entity annotation. Hibernate considers class with @Entity annotation as eligible to be persisted to the database.

import javax.persistence.Entity;
@Entity
public class Employee {
    // Fields and getter/setter
}

 

Table Mapping with @Table

By default, table names are derived from the entity names. Therefore, given a class Employee with a simple @Entity annotation, the table name would be “employee”.

If we want a table name different that the Class name, we can specify the same using @Table annotation.

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="employee_details")
public class Employee {

   // Fields and getter/setter
}

 

Mapping Properties and Fields with @Column

We can use @Column attribute to map a property in the Entity class to a column in the table.

It is optional, but is often useful when you need to fit your object model into a preexisting schema.

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="employee_details")
public class Employee {

  @Column
  private String name;
}

If we need a custom column name, we can use the name attribute as shown :

@Column(name = "emp_name")
private String name;

 
Here are some useful attributes available for the @Column annotation.

length

The length attribute is used to provide the column with a maximum size.

nullable

The nullable attribute accepts a Boolean value. If nullable is true, it means that the column contains a NULL value.

unique

The unique attribute also accepts a Boolean value. If unique is set to true, hibernate will create a column with a UNIQUE index. However, the default value is false.

  @Column(name = "emp_name",length=50,nullable=false)
  private String name;
 

 

Primary Keys with @Id and @GeneratedValue

The @Id annotation is used to declare a column as the Primary key.

import javax.persistence.Id;
@Entity
public class Employee {

  @Id
  private int id;
}

 

We can create a primary key with some autogenerated value using the @GeneratedValue annotation.

This takes a pair of attributes: strategy and generator.

The strategy attribute must be a value from the javax.persistence.GeneratorType enumeration.

There are four different types of primary key generators on GeneratorType, as follows:

  1. AUTO:

    Hibernate decides which generator type to use, based on the database’s support for primary key generation.

    By default, hibernate uses the GenerationType.AUTO strategy if no strategy is supplied.

    So, @GeneratedValue is equal to @GeneratedValue(strategy=GenerationType.AUTO).

      @Id
      @GeneratedValue
      private int id;
    

    This is same as :

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
    
  2.  

  3. IDENTITY:

    The database is responsible for determining and assigning the next primary key.

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
    
  4.  

  5. SEQUENCE:

    If sequence is supported in the database, we can create it and add the mapping in the Java code, as shown here :

        @Id
        @SequenceGenerator(name="seq", sequenceName="DB_SEQ")
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
        private long id;
    
  6.  

  7. TABLE:

    In a table generator, the value for the primary key column is stored in one table.

        @Id
        @TableGenerator(name="tablegen", table="ID_TABLE", pkColumnName="ID", valueColumnName="NEXT_ID")
        @GeneratedValue(strategy=TABLE,generator="tablegen")
        int id;
    

 

Composite keys with @IdClass

This annotation is used when the primary key is composed of multiple keys, i.e, it’s a composite primary key.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

@Entity
@IdClass(Employee.class)
public class Employee {
  @Id
  private String name;
  @Id
  private int age;
}

Here, we annotated the name and age columns with @Id, which means that we want to create a primary key for both. Also, we annotated the entity class with the @IdClass annotation.

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags