Annotations Support

Annotations Support

Hibernate has provided annotations for almost all the primary key generation algorithm in the form of javax.persistence package and org. hibernate.annotation package.

To provide annotations for primary key generation algorithm javax.persistence package has provided the following annotations.

  • @GeneratedValue(–,–)
  • @SequenceGenerator(–)
  • @TableGenerator(–)

To provide a particular primary key generation algorithm in hibernate applications, we have to use “strategy” member in @GeneratedValue(-) annotation, it will take either of the following constants from “GenerationType” Enum.

  1. IDENTITY
  2. SEQUENCE
  3. TABLE
  4. AUTO
1. IDENTITY

This value of GenerationType enum will represent IdentityGenerator or “identity” primary key generation algorithm to generate primary key value on the basis of the underlying database provided identity column.

E.g

package com.ashok.hibernate.annotations.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name = "emp")
public class Employee {
	@Id
	@Column(name = "emp_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private String empId;
	@Column(name = "emp_name")
	private String empName;
	@Column(name = "address")
	private String address;
	@Column(name = "salary")
	private double salary;

	public Employee() {
		super();
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
}
2. SEQUENCE

This constant from GenerationType enum is able to represent
SequenceGenerator or “sequence” primary key generation algorithm in order to generate primary key value on the basis of the sequence which we defined in database.

To configure “sequence” name we have to use “@SequenceGenerator” annotation with the following members.

  1. name: It will take logical name of the @SequenceGenerator.
  2. sequenceName: it will take “sequence” name provided by underlying database.

To apply @SequenceGenerator to @GeneratedValue annotation we have to use “generator” member in “@GeneratedValue” annotation.

E.g

package com.ashok.hibernate.annotations.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name = "emp")
public class Employee {
	@Id
	@Column(name = "emp_id")
	@SequenceGenerator(name = "seqGen", sequenceName = "my_sequence")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGen")
	private String empId;
	@Column(name = "emp_name")
	private String empName;
	@Column(name = "address")
	private String address;
	@Column(name = "salary")
	private double salary;

	public Employee() {
		super();
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
}

Note

In the above example, if we remove @SequenceGenerator annotation and “generator” member in @GeneratedValue annotation then Hibernate software will search for the default sequence name like “hibernate_sequence” in the database.

3. TABLE

This constant from GenerationType enum is able to represent HiLo Primary key generation algorithm in order to generate primary key values on the basis of Global resource. To configure Global resource table and its properties we have to use @TableGenerator() annotation with the following members.

  1. name: it will take logical name to the @TableGenerator
  2. pkColumnName: it will take primary key column name of the Global resource table.
  3. pkColumnValue: It will take initial value to the primary key column in global resource table.
  4. valueColumnName: it will take a column name which is generating our required primary key value.
  5. table: it will take Global resource table name.

To apply @TableGenerator annotation to @GeneratedValue annotation we have to use “generator” member in @GeneratedValue annotation.

package com.ashok.hibernate.annotations.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name = "emp")
public class Employee {
	@Id
	@Column(name = "emp_id")
	@TableGenerator(name = "tableGen", table = "my_table", pkColumnName = "id", pkColumnValue = "10", valueColumnName = "next_hi")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "tableGen")
	private String empId;
	@Column(name = "emp_name")
	private String empName;
	@Column(name = "address")
	private String address;
	@Column(name = "salary")
	private double salary;

	public Employee() {
		super();
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
}
4. AUTO

This constant from GenerationType enum is able to represent “native” primary key generation algorithm in order to generate primary key value by selecting either sequence or identity or hilo primary key generation algorithms on the basis of the under lying database.

Note

Depending on the database or depending on the internal primary key generation algorithm we have to provide input parameters by using @SequenceGenerator() or @TableGenerator() annotations.

E.g

package com.ashok.hibernate.annotations.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name = "emp")
public class Employee {
	@Id
	@Column(name = "emp_id")
	@SequenceGenerator(name = "seqGen", sequenceName = "my_sequence")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seqGen")
	private String empId;
	@Column(name = "emp_name")
	private String empName;
	@Column(name = "address")
	private String address;
	@Column(name = "salary")
	private double salary;

	public Employee() {
		super();
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
}

Note

For the remaining primary key generation algorithm, Hibernate has provided the following annotation in org.hibernate.annotation package.

@GenericGenerator(name=”–“, strategy=”–“,parameters={…})
@GeneratedValue(generator=”–“)

  1. name: It will take logical name to @GenericGenerator
  2. strategy: It will take short name of the primary key generation alg.
  3. parameters: It will take array of @Parameter() with the following members to declare parameter values.
  4. name: it will take parameter name.
  5. value: It will take parameter value.
Annotations Support
Scroll to top