Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating and Configuring Entities"

From Eclipsepedia

Jump to: navigation, search
(Configuring an Entity's Persistent Identity)
m (Replacing page with ' '''Warning See "Understanding Entities" in the [http://www.eclipse.org/eclipselink/documentation/ EclipseLink Concepts Guide]'''')
(17 intermediate revisions by one user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
 
|info=y
 
|toc=n
 
|eclipselink=y
 
|eclipselinktype=JPA
 
|api=y
 
|apis=
 
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Entity.html @Entity]
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Id.html @Id]
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/IdClass.html @IdClass]
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/package-summary.html Package javax.persistence]
 
|nativeapi=y
 
|nativeapis=
 
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html ClassDescriptor]}}
 
  
= Configuring Entities=
 
  
An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in the table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.
 
  
The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store.
+
'''[[Image:Elug_draft_icon.png|Warning]] See "Understanding Entities" in the [http://www.eclipse.org/eclipselink/documentation/ EclipseLink Concepts Guide]'''
 
+
== Identifying an Entity ==
+
Use the <tt>@Entity</tt> annotation to specify that a class is an entity, as shown in the following example:
+
 
+
<source lang="java">
+
@Entity
+
public class Employee implements Serializable {
+
...
+
}
+
</source>
+
 
+
{{EclipseLink_Note|note=The entity class must also be listed in your <tt>persistence.xml</tt> file, unless you set the tag <code><exclude-unlisted-classes></code> to <code>false</code>.  An entity can also be defined using an <tt>orm.xml</tt> file and the <code><entity></code> tag.}}
+
 
+
{{EclipseLink_Spec
+
|link=http://jcp.org/en/jsr/detail?id=220
+
|section=Chapter 2 "Entities"}}
+
 
+
==Configuring an Entity's Persistent Identity==
+
Every entity must have a persistent identity, which is an equivalent of a primary key in a database table that stores the entity state.
+
 
+
By default, the EclipseLink persistence provider assumes that each entity has at least one field or property that serves as a primary key.
+
 
+
You can generate and/or configure the identity of your entities by using the following annotations:
+
 
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/Id|@Id]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/IdClass|@IdClass]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/EmbeddedId|@EmbeddedId]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|@GeneratedValue]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/TableGenerator|@TableGenerator]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator|@SequenceGenerator]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue#Advanced Identity Generation|Advanced Identity Generation]]
+
 
+
You can also use these annotations to fine-tune how your database maintains the identity of your entities.
+
 
+
{{EclipseLink_Note|note=If your class does not have a logical Id then you need to add a generated one.  Embedded objects do not require an Id.}}
+
<br/>
+
 
+
==Configuring an Entity's Table==
+
Every entity class maps to a specific table or set of tables in the database.  By default the entity's table is defaulted as its entity name, which defaults to the entity's short class name.  An entity normally maps to a single table, but can also map to multiple tables, or even a view.
+
 
+
You can customize an entity's tables using the following annotations:
+
 
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Table|@Table]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/SecondaryTable|@SecondaryTable]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/SecondaryTable#Advanced Table Configuration|Advanced Table Configuration]]
+
 
+
<br>
+
<br>
+
 
+
{{EclipseLink_JPA
+
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Configuration/JPA/Overriding_and_Merging|Overriding and Merging Metadata Settings]]
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development|Basic JPA Development]]
+
|next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entities/Ids/Id|@Id]]
+
|version=2.2.0 DRAFT}}
+

Revision as of 11:53, 25 January 2013


Warning See "Understanding Entities" in the EclipseLink Concepts Guide