Every JPA entity must have a primary key.
Aug 02, 2014 Primary key generation in JPA Posted on August 2, 2014 by Robert Piasecki Generation of primary key values is a very important functionality of relational database management systems.
You can specify a primary key as a single primitive, or JDK object type entity field (see 'Configuring a JPA Entity Simple Primary Key Field').
You can specify a composite primary key made up of one or more primitive, or JDK object types using a separate composite primary key class (see 'Configuring a JPA Entity Composite Primary Key Class').
You can either assign primary key values yourself, or you can associate a primary key field with a primary key value generator (see 'Configuring JPA Entity Automatic Primary Key Generation').
Configuring a JPA Entity Simple Primary Key Field
The simplest primary key is one you specify as a single primitive or JDK object type entity field (see 'Using Annotations').
Note:
For a JPA entity primary key field code example, see: http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html#id
Using Annotations
Example 7-1 shows how to use the
@Id annotation to specify an entity field as the primary key. In this example, primary key values are generated using a table generator (see 'Configuring JPA Entity Automatic Primary Key Generation').
Configuring a JPA Entity Composite Primary Key Class
A composite primary key is usually made up of two or more primitive or JDK object types. Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns. You can specify such a composite primary key with a separate composite primary key class (see 'Using Annotations')
A composite primary key class has the following characteristics:
You can make the composite primary key class either an embedded class owned by the entity class, or a nonembedded class whose fields you map to multiple fields or properties of the entity class. In the latter case, the names of primary key fields or properties in the composite primary key class and those of the entity class must correspond and their types must be the same.
Using Annotations
Example 7-2 shows a typical embeddable composite primary key class. Example 7-3 shows how to configure a JPA entity with this embedded composite primary key class using the
@EmbeddedId annotation.
Example 7-2 Embeddable Composite Primary Key Class
Example 7-3 JPA Entity With an Embedded Composite Primary Key Class
Example 7-5 shows a nonembedded composite primary key class. In this class, fields
empName and birthDay must correspond in name and type to properties in the entity class. Example 7-5 shows how to configure a JPA entity with this nonembedded composite primary key class using the @IdClass annotation. Because entity class fields empName and birthDay are used in the primary key, you must also annotate them using the @Id annotation.
Example 7-4 Non-Embedded Composite Primary Key Class
Example 7-5 JPA Entity With a Mapped Composite Primary Key Class
Configuring JPA Entity Automatic Primary Key Generation
Typically, you associate a primary key field (see 'Configuring a JPA Entity Simple Primary Key Field') with a primary key value generator so that when an entity instance is created, a new, unique primary key value is assigned automatically.
Table 7-2 lists the types of primary key value generators that you can define.
Table 7-2 JPA Entity Primary Key Value Generators
Note:
For an EJB 3.0 automatic primary key generation code example, see: http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html#sequencing
Using Annotations
Example 7-6 shows how to use the
@TableGenerator annotation to specify a primary key value generator based on a database table. The TopLink JPA persistence provider will attempt to create this table at deployment time: if it cannot, then you must follow your database documentation to ensure that this table exists before deployment. When a new instance of Address is created, a new value for entity field id is obtained from ADDRESS_GENERATOR_TABLE . In this case, you must set the @GeneratedValue annotation attribute strategy to TABLE and generator to ADDRESS_TABLE_GENERATOR .
Example 7-6 GeneratedValue Strategy Table: @TableGenerator
Example 7-7 shows how to use the
@SequenceGenerator annotation to specify a primary key value generator based on a sequence object provided by the database. The TopLink JPA persistence provider will attempt to create this object at deployment time: if it cannot, then you must follow your database documentation to ensure that this sequence object exists before deployment. When a new instance of Address is created, a new value for entity field id is obtained from database sequence object ADDRESS_SEQ . In this case, you must set the @GeneratedValue annotation attribute strategy to SEQUENCE and generator to ADDRESS_SEQUENCE_GENERATOR .
Example 7-7 GeneratedValue Strategy Sequence: @SequenceGenerator
Example 7-8 shows how to use the
@GeneratedValue annotation to specify a primary key value generator based on a primary key identity column (autonumber column). When a new instance of Address is persisted, the database assigns a value to the identity column. In this case, the TopLink JPA persistence provider re-reads the inserted row and updates the in-memory Address entity to set id to this value.
How to Configure Primary Key Generation
This document describes how to configure primary key generation with EclipseLink JPA. See @Generated value in the EclipseLink User's Guide for complete information.
Introduction
EclipseLink may create entity identifiers (or primary keys) automatically using any of the following strategies defined by the JPA Specification:
Usually, these generation strategies are configured locally to the primary key field or property.
![]() Using Sequence Objects
When using a database that supports sequence objects (such as Oracle Database), EclipseLink can use a database sequence object to automatically generate identifiers for your persistent objects.
Using A Default Sequence
EclipseLink can produce a default sequence during schema generation. If you use schema generation, then specify that your identifier should be generated and that the SEQUENCE strategy be used to perform the generation.
In the following example, the @GeneratedValue annotation indicates that the identifier value should be automatically generated; a strategy of SEQUENCE indicates that a database sequence should be used to generate the identifier. EclipseLink will create a default sequence object during schema generation that will be used at run time.
Specifying a Sequence
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. You may choose any unique label as the name for the sequence generator. Reference this name by the generator element in the @GeneratedValue annotation. Also, include the sequenceName element to specify the name of the database sequence object to use.
If the sequence object already exists in the database, then you must specify the allocationSize to match the INCREMENT value of the database sequence object. For example, if you have a sequence object in the database that you defined to INCREMENT BY 5, set the allocationSize to 5 in the sequence generator definition, as the following example shows:
Using Identity Columns
When using a database that does not support sequences, but does support identity columns (such as SQL Server database), you can configure JPA to use an identity column to generate identifiers.
To generate identifiers using identity columns, specify a strategy of IDENTITY. In the following example, the @GeneratedValue annotation indicates that the identifier value should be automatically generated, and the specified strategy of IDENTITY indicates that an identity column should be used to generate the identifier:
Using a Table
You can use a table for identifier generation on any database. This strategy is completely portable across databases and will be automatically generated for you when schema generation is enabled.
Using A Default Table
During schema generation, EclipseLink JPA can generate a default table for identifier generation. If you use schema generation, then specify a strategy of TABLE in the @GeneratedValue annotation, as the following example demonstrates. EclipseLink will create a default table during schema generation that will be used at run time:
Specifying a Table
To map to an existing table or cause the table object generated by schema generation to be given a particular name, define a table generator using a @TableGenerator annotation.
Foreign Key
The table generator has a name, by which it is referenced in the @GeneratedValue annotation. The generator also lists the name of the specific database table, as well as the names of the key and value columns used to store identifier generators in the table. Each row in the table represents the generator for a particular entity type, with the value in the key column indicating the entity type.
For example, a table generator for Inventory instances might have a key of INV_GEN, as the following example shows:
The table generator defined in the preceding example would be mapped to the following table:
Using a Default Generation Strategy
Specifying a strategy of AUTO allows EclipseLink to select the strategy to use. Typically, EclipseLink picks TABLE as the strategy, since it is the most portable strategy. However, when AUTO is specified, schema generation must be used at least once in order for the default table to be created in the database.
The following example demonstrates the use of the AUTO strategy:
Using a Custom Strategy
You can also choose to implement your own sequencing. An example using UUID is provided here.
Jpa Composite Key Sequence Generator FreeSummary
The minimum configuration you can use to cause the automatic generation of identifiers is to add a @GeneratedValue annotation to the identifier field or property. If you are using a specific named database sequence or table, you need to define the generator in the metadata with @SequenceGenerator or @TableGenerator annotations.
Example Of Composite Key
The generation strategy that you choose to generate entity identifiers may depend on the database, on which you application is running: for instance, SEQUENCE and IDENTITY strategies are not supported on all databases.
Retrieved from 'https://wiki.eclipse.org/index.php?title=EclipseLink/Examples/JPA/PrimaryKey&oldid=316256'
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |