Java Reference
In-Depth Information
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
public void setId(Long id) { = id;
//Other generated methods (equals(), hashCode(), toString())
omitted for brevity
As we can see, a JPA entity is a standard Java object, there is no need to extend any
special class or implement any special interface. What differentiates a JPA entity
from other Java objects are a few JPA-specific annotations.
The @Entity annotation is used to indicate that our class is a JPA entity. Any object
we want to persist to a database via JPA must be annotated with this annotation.
The @Id annotation is used to indicate what field in our JPA entity is its primary key.
The primary key is a unique identifier for our entity. No two entities may have the
same value for their primary key field. This annotation can be placed just above the
getter method for the primary key class, this is the strategy that the NetBeans wizard
follows; it is also correct to specify the annotation right above the field declaration.
The @Entity and the @Id annotations are the bare minimum two annotations that
a class needs in order to be considered a JPA entity. JPA allows primary keys to
be automatically generated. In order to take advantage of this functionality, the @
GeneratedValue annotation can be used, as we can see, the NetBeans generated JPA
entity uses this annotation. This annotation is used to indicate the strategy to use to
generate primary keys. All possible primary key generation strategies are listed in
the following table:
Primary Key Generation Strategy Description
Indicates that the persistence provider will
automatically select a primary key generation strategy.
Used by default if no primary key generation strategy
is specified.
Indicates that an identity column in the database table
the JPA entity maps to must be used to generate the
primary key value.
Search WWH ::

Custom Search