2.5.2.4. Generated Value
The previous section showed you how to declare your identity fields with the Id annotation.
It is often convenient to allow the persistence implementation to assign a unique value to your identity fields automatically.
JPA includes the GeneratedValue annotation for this purpose.
It has the following properties:
-
GenerationType strategy: Enum value specifying how to auto-generate the field value. TheGenerationTypeenum has the following values:-
GeneratorType.AUTO: The default. Assign the field a generated value, leaving the details to the JPA vendor. If the field type is UUID, it will be given an generated UUID. -
GenerationType.IDENTITY: The database will assign an identity value on insert. -
GenerationType.SEQUENCE: Use a datastore sequence to generate a field value. -
GenerationType.TABLE: Use a sequence table to generate a field value. -
GenerationType.UUID: Assign the field a type 4 UUID generated value. In this case, the field must be eitherStringorUUID.
-
-
String generator: The name of a generator defined in mapping metadata. We show you how to define named generators in Generators. If theGenerationTypeis set but this property is unset, the JPA implementation uses appropriate defaults for the selected generation type.
The equivalent XML element is generated-value, which includes the following attributes:
-
strategy: One ofTABLE,SEQUENCE,IDENTITY, orAUTO, defaulting toAUTO. -
generator: Equivalent to the generator property listed above.
|
|
OpenJPA allows you to use the OpenJPA also offers additional generator strategies for non-numeric fields, which you can access by setting
These string constants are defined in org.apache.openjpa.persistence.Generator. If the entities are mapped to the same table name but with different schema name within one |