Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/Id"

m
Line 7: Line 7:
 
|apis=
 
|apis=
 
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @Id]
 
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @Id]
 +
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @IdClass]
 +
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @GeneratedValue]
 
}}
 
}}
 
=@Id=
 
=@Id=
Use the <tt>@Id</tt> annotation to designate one or more persistent fields or properties as the entity's primary key.
+
Use the <tt>@Id</tt> annotation or <code><id></code> 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:
 
For each entity, you must designate at least one of the following:
Line 24: Line 26:
  
 
By default, the entities <code>Id</code> must be set by the application, normally before the <code>persist</code> is called.  A [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/GeneratedValue|@GeneratedValue]] can be used to have EclipseLink generate the Id value.
 
By default, the entities <code>Id</code> must be set by the application, normally before the <code>persist</code> is called.  A [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/GeneratedValue|@GeneratedValue]] can be used to have EclipseLink generate the Id value.
 +
 +
If the <code>Id</code> (or part of the Id) is also a foreign key from a reference to another entity, the <tt>@Id</tt> annotation should be placed on the <tt>@OneToOne</tt> or <tt>@ManyToOne</tt> attribute.  The entity's <code>Id</code> is then composed of the <code>Id</code> of the referenced entity.
  
 
This example shows how to use this annotation to designate the persistent field <tt>empID</tt> as the primary key of the <tt>Employee</tt> table.
 
This example shows how to use this annotation to designate the persistent field <tt>empID</tt> as the primary key of the <tt>Employee</tt> table.
Line 31: Line 35:
  
 
<source lang="java">
 
<source lang="java">
@Entity
+
@Entity
public class Employee implements Serializable {
+
public class Employee implements Serializable {
    @Id
+
    @Id
    private int empID;
+
    private int empID;
    ...
+
    ...
}
+
}
 +
</source>
  
 +
<span id="Id_XML_Example"></span>
 +
======''Example: Id XML''======
 +
 +
<source lang="xml">
 +
<entity class="Employee">
 +
    <attributes>
 +
        <id name="empID"/>
 +
        ...
 +
    </attributes>
 +
</entity>
 
</source>
 
</source>
  
The <tt>@Id</tt> annotation supports the use of EclipseLink converters (see [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Using EclipseLink JPA Converters]]).
+
The <tt>@Id</tt> annotation supports the use of EclipseLink converters. See [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Using EclipseLink JPA Converters]].
 +
 
 +
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
 
|link=http://jcp.org/en/jsr/detail?id=220
 
|link=http://jcp.org/en/jsr/detail?id=220
 
|section=Section 11.1.18 "Id Annotation"}}
 
|section=Section 11.1.18 "Id Annotation"}}
 
  
  

Revision as of 11:04, 8 June 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

@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:

Elug note icon.png

Note: The last option in the preceding list – @Id and @IdClass combination – is 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.

Elug javaspec icon.gif

For more information, see Section 11.1.18 "Id Annotation" in the JPA Specification.


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top