JPA primary key generator and primary key generation policy

Source: Internet
Author: User
Tags db2 generator postgresql oracle database

When you create an entity in JPA, you declare the primary key of the entity and its primary key generation policy. We have an entity class called email, whose primary key is declared as follows:

@Id
@Column (name = "email_id")
@GeneratedValue (strategy = generationtype.sequence, generator = "Emailseq")
@SequenceGenerator (initialvalue = 1, name = "Emailseq", Sequencename = "email_sequence")
private long ID;

We use @generatedvalue's strategry field to declare the primary key generation policy, generator declares the name of the primary key generator, corresponding to the primary key generator @sequencegenerator or @tablegenerator with the same name.

Unlike Hibernate, JPA provides only four primary key generator policies, as described below: generationtype.identity

Most databases support identity columns, which are automatically assigned to IDs when new rows are inserted, which is also called ID self-growth columns, such as the ability to declare "auto_increment" in MySQL when creating a table, or an ID child growth column:

CreateTable email{
    ID BIGINT not NULL auto_increment, MESSAGE VARCHAR (255) NOT NULL, PRIMARY KEY (ID)
}


The primary key generation policy for identity types in JPA is as follows:

@GeneratedValue (strategy=generationtype.identity)
@Column (name= "id")
private long ID;

Because the primary key is automatically inserted by the database, no additional configuration information is required.

Most databases support the identity policy: MySQL, SQL Server, DB2, Derby, Sybase, PostgreSQL. generationtype.sequence

Oracle does not support the ID sub-growth column but instead uses the mechanism of the sequence to generate the primary key ID, which allows the sequence to be used as the primary key generation strategy:

@GeneratedValue (strategy = generationtype.sequence, generator = "Emailseq")
@SequenceGenerator (InitialValue = 1, Name = "Emailseq", Sequencename = "email_sequence")
private long ID;;

The above declaration is equivalent to creating a sequence on the database:

Create sequence email_sequence;


If you do not specify the name of the sequence builder, the default sequence builder provided by the vendor is used, such as Hibernate, which provides the sequence name hibernate_sequence.

Supported databases: Oracle, PostgreSQL, DB2 generationtype.table

Sometimes in order to not rely on the specific implementation of the database, a better migration between the different databases, you can create a new sequence table in the database to generate the primary key, the sequence table typically contains two fields: the first field refers to a different relationship table, the second field is the maximum ordinal of the relationship table. This allows a single sequence to be used for the primary key generation of multiple tables. Usage:

@TableGenerator (name = "Emailseq", table = "My_project_sequence_table", Pkcolumnname = "Sequence_name", Valuecolumnname  = "Sequence_count", InitialValue = 1, allocationsize = 1)
@GeneratedValue (strategy = generationtype.table, generator = "Emailseq")

If you do not specify a table generator, the JPA vendor uses the default table, such as Hibernate, which uses the default table Hibernate_sequence on the Oracle database.

Although this approach is best-in-the-way, all relational databases are supported, but due to the inability to fully utilize the specific database features, it is not recommended to use them preferentially. Generationtype.auto

The primary key generation strategy is given to the JPA vendor (Persistence Provider), which chooses the appropriate strategy based on the specific database, which can be one of the table/sequence/identity. If the database is Oracle, select sequence.

If not specifically specified, this is the default primary key generation policy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.