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.