Configuring JPA Primary key sequence generators
Posted
by pachunoori.vinay.kumar(at)oracle.com
on Oracle Blogs
See other posts from Oracle Blogs
or by pachunoori.vinay.kumar(at)oracle.com
Published on Wed, 15 Dec 2010 15:06:39 +0530
Indexed on
2010/12/16
4:14 UTC
Read the original article
Hit count: 597
This article describes the JPA feature of generating and assigning the unique sequence numbers to JPA entity .This article provides information on jpa sequence generator annotations and its usage.
UseCase Description
Adding a new Employee to the organization using Employee form should assign unique employee Id.
Following description provides the detailed steps to implement the generation of unique employee numbers using JPA generators feature
Steps to configure JPA Generators
1.Generate Employee Entity using "Entities from Table Wizard".
View image
2.Create a Database Connection and select the table "Employee" for which entity will be generated and Finish the wizards with default selections. View image
3.Select the offline database sources-Schema-create a Sequence object or you can copy to offline db from online database connection.
4.Open the persistence.xml in application navigator and select the Entity "Employee" in structure view and select the tab "Generators" in flat editor.
5.In the Sequence Generator section,enter name of sequence "InvSeq" and select the sequence from drop down list created in step3.
View image
6.Expand the Employees in structure view and select EmployeeId and select the "Primary Key Generation" tab.
7.In the Generated value section,select the "Use Generated value" check box ,select the strategy as "Sequence" and select the Generator as "InvSeq" defined step 4.
Following annotations gets added for the JPA generator configured in JDeveloper for an entity
To use a specific named sequence object (whether it is generated by schema generation or already exists in the database) you must define a sequence generator using a @SequenceGenerator annotation. Provide a unique label as the name for the sequence generator and refer the name in the @GeneratedValue annotation along with generation strategy
For example,see the below Employee Entity sample code configured for sequence generation.
EMPLOYEE_ID is the primary key and is configured for auto generation of sequence numbers.
EMPLOYEE_SEQ is the sequence object exist in database.This sequence is configured for generating the sequence numbers and assign the value as primary key to Employee_id column in Employee table.
@SequenceGenerator(name="InvSeq", sequenceName = "EMPLOYEE_SEQ")
@Entity
public class Employee implements Serializable {
@Id
@Column(name="EMPLOYEE_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="InvSeq")
private Long employeeId;
}
- @SequenceGenerator
- @GeneratedValue
@SequenceGenerator - will define the sequence generator based on a database sequence object
Usage: @SequenceGenerator(name="SequenceGenerator", sequenceName = "EMPLOYEE_SEQ")
@GeneratedValue - Will define the generation strategy and refers the sequence generator
Usage:
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="name of the Sequence generator defined in @SequenceGenerator")
© Oracle Blogs or respective owner