Java Reference
In-Depth Information
Entity identity
A persistent entity maps to a database table and must have a persistent identity that
is an equivalent of the primary key in the database table that stores the entity state.
The EclipseLink persistence provider assumes that each entity has at least one field/
property that is the primary key. A primary key field using the @Id annotation is
specified as follows:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
The @GeneratedValue annotation specifies that the EclipseLink persistence provider
generate unique identifiers for entity primary keys. The strategy attribute is not
required and the default value is AUTO. The @GeneratedValue annotation is not
required and by default the EclipseLink JPA persistence provider chooses the most
appropriate primary key generator. We shall be using the default primary
key generator:
@Id
private int id;
Entity relationships
We shall be using the @OneToMany , @ManyToOne , and @ManyToMany annotations to
specify relationships between entities. The fetch strategy in an entity relationship
specifies if the associated entities are fetched when an entity is fetched, and its value
may be FetchType.LAZY or FetchType.EAGER . For the LAZY strategy, associated
entities are not fetched and for the EAGER strategy, associated entities are fetched.
The default values are listed in the following table:
Relationship
Default Fetch Strategy
@OneToMany
LAZY
@ManyToOne
EAGER
@ManyToMany
LAZY
Search WWH ::




Custom Search