Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/Id"
(→@Id) |
(→Example: Id XML) |
||
Line 58: | Line 58: | ||
It is possible to generate the value for an <code>Id</code> using the <code>prePersist</code> event, or through triggers when using the Oracle database platform. See [[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Returning|Returning]]. | It is possible to generate the value for an <code>Id</code> using the <code>prePersist</code> event, or through triggers when using the Oracle database platform. See [[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Returning|Returning]]. | ||
+ | |||
{{EclipseLink_Spec | {{EclipseLink_Spec |
Revision as of 11:06, 8 June 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
@Id
Use the @Id annotation or <id>
XML element to designate one or more persistent fields or properties as the entity's primary key.
For each entity, you must designate at least one of the following:
- one @Id, or
- one @EmbeddedId, or
- multiple @Id and an @IdClass
Note: The @Id and @IdClass combination – is only applicable to composite primary key configuration.
The @Id annotation does not have attributes.
By default, the entities Id
must be set by the application, normally before the persist
is called. A @GeneratedValue can be used to have EclipseLink generate the Id value.
If the Id
(or part of the Id) is also a foreign key from a reference to another entity, the @Id annotation should be placed on the @OneToOne or @ManyToOne attribute. The entity's Id
is then composed of the Id
of the referenced entity.
This example shows how to use this annotation to designate the persistent field empID as the primary key of the Employee table.
Example: @Id Annotation
@Entity public class Employee implements Serializable { @Id private int empID; ... }
Example: Id XML
<entity class="Employee"> <attributes> <id name="empID"/> ... </attributes> </entity>
The @Id annotation supports the use of EclipseLink converters. See Using EclipseLink JPA Converters.
It is possible to generate the value for an Id
using the prePersist
event, or through triggers when using the Oracle database platform. See Returning.
For more information, see Section 11.1.18 "Id Annotation" in the JPA Specification.