The @Id
property should be automated
populated by the database, rather than by Java code. What this means
that a @GeneratedValue
should be
specified.
This restriction may be lifted in future versions of JPA Objects.
For example:
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.nakedobjects.applib.annotation.Hidden; ... public class Customer { // {{ Id private Long id; @Hidden @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) public Long getId() { return id; } public void setId(Long id) { this.id = id; } // }} ... }
Because the Id key is automatically generated it generally has no
meaning to the end-user, and therefore should also be annotated as
@Hidden
. This isn't a requirement of
JPA Objects, however.
The value of the @GeneratedValue
's strategy
is a GenerationType
:
TABLE
Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
For this generation type, the
@TableGenerator
can also be specified. This
annotation defines a primary key generator that may be referenced by
name when a generator element is specified for the GeneratedValue
annotation. A table generator may be specified on the entity class
or on the primary key field or property. The scope of the generator
name is global to the persistence unit (across all generator
types).
SEQUENCE
Indicates that the persistence provider must assign primary keys for the entity using database sequence column.
For this generation type, the
@SequenceGenerator
can also be specified.
This annotation defines a primary key generator that may be
referenced by name when a generator element is specified for the
GeneratedValue annotation. A sequence generator may be specified on
the entity class or on the primary key field or property. The scope
of the generator name is global to the persistence unit (across all
generator types).
IDENTITY
Indicates that the persistence provider must assign primary keys for the entity using database identity column.
AUTO
Indicates that the persistence provider should pick an
appropriate strategy for the particular database. The
AUTO
generation strategy may expect a database
resource to exist, or it may attempt to create one. A vendor may
provide documentation on how to create such resources in the event
that it does not support schema generation or cannot create the
schema resource at runtime.
To capture the ID for use within the domain object, see Section 8.2, “Lifecycle Listeners”.