Using Apache OPENJPA to develop EJB 3.0 applications, part 5th: Automatic generation of entity identities

Source: Internet
Author: User

The uniqueness of the data is a very basic requirement for all applications, and there is a greater risk that the developer or user will maintain this uniqueness, so it is common practice for the system to automatically generate unique identities. Four different entity identities are supported automatically generation policies in OpenJPA:

The entity identification automatically generated by the container;

Generate entity identities using the automatic growth field of the database;

The entity identification is generated according to the database serial number (Sequence) technology;

Generate entity identities using fields from database tables;

Each of these four ways has advantages and disadvantages, and developers can choose according to the actual situation.

selectable annotations

To allow the container and database to combine the automatic generation of the management entity identity, depending on the actual situation, the developer can choose the Generatedvalue, Sequencegenerator, and Tablegenerator under the javax.persistence.* package Three annotations to describe the identity field of an entity.

@javax. Persistence.generatedvalue

Each entity that needs to automatically generate an entity identity needs to provide generatedvalue comments and corresponding parameters for its entity identification fields, and the OpenJPA framework handles automatic generation of entity identities based on annotations and parameters.

Entity identities that are automatically generated by using Generatedvalue annotations can be numeric type fields such as Byte, short, int, long, or their corresponding wrapper type Byte, short, Integer, long, or string type.

Generatedvalue annotations can support two properties strategy and generator.

Strategy

Strategy is an enumeration value of type GenerationType that specifies how the OpenJPA container automatically generates entity identities. The Strategy property can be the following enumeration value:

Generatortype.auto

Indicates that the entity identity is automatically generated by the OpenJPA container, which is also the default value for the Strategy property.

Generationtype.identity

The OpenJPA container assigns unique values to the newly added entity objects using the database's self-growth field as an identity for the entity. In this case, the database is required to provide support for the growth field, which is supported by databases such as Hsql, SQL Server, MySQL, DB2, and Derby in common databases.

Generationtype.sequence

Represents the use of the database's serial number to assign a unique value to the newly added entity object as the identity of the entity. In this case, the database is required to provide support for serial numbers, and databases such as Oracle and PostgreSQL can provide such support in common databases.

Generationtype.table

Represents a field that uses a specified table in the database to record the identity of an entity object, with the growth of that field assigning a unique value to the newly added entity object as the identity of the entity.

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.