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.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Annotations"
m |
|||
(13 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|api=y | |api=y | ||
|apis= | |apis= | ||
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/ | + | * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Entity.html @Entity] |
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/ | + | * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/package-summary.html javax.persistence] |
− | * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/package-summary.html annotations] | + | * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/package-summary.html org.eclipse.persistence.annotations] |
− | + | |examples=y | |
+ | |example= | ||
+ | * [http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/Employee Employee JPA 2] | ||
|1=*javax.persistence | |1=*javax.persistence | ||
|2=*org.eclipselink.annotations | |2=*org.eclipselink.annotations | ||
− | }} | + | }} |
− | =Annotations= | + | |
+ | = Adding Metadata Using Annotations = | ||
+ | |||
An annotation is a simple, expressive means of decorating Java source code with metadata that is compiled into the corresponding Java class files for interpretation at run time by a JPA persistence provider to manage persistent behavior. | An annotation is a simple, expressive means of decorating Java source code with metadata that is compiled into the corresponding Java class files for interpretation at run time by a JPA persistence provider to manage persistent behavior. | ||
− | A metadata annotation represents a Java language feature that lets you attach structured and typed metadata to the source code. Annotations alone are sufficient for the metadata specification -- you do not need to use XML. | + | A metadata annotation represents a Java language feature that lets you attach structured and typed metadata to the source code. Annotations alone are sufficient for the metadata specification -- you do not need to use XML. Standard JPA annotations are in the [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/package-summary.html <tt>javax.persistence</tt>] package. {{EclipseLink_Spec |
− | {{EclipseLink_Spec | + | |
|link=http://jcp.org/en/jsr/detail?id=220 | |link=http://jcp.org/en/jsr/detail?id=220 | ||
|section=Chapter 10 "Metadata Annotations" | |section=Chapter 10 "Metadata Annotations" | ||
− | }} | + | }} |
− | An object relational mapping XML file is optional. If you choose to provide one, then it should contain mapping information for the classes listed in it. The persistence provider loads an [[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/orm.xml|orm.xml file]] (or other mapping file) as a resource. If you provide a mapping file, the classes and mapping information specified in the mapping file will be used. The XML mapping metadata may combine with and override annotation metadata. | + | An object relational mapping XML file is optional. If you choose to provide one, then it should contain mapping information for the classes listed in it. The persistence provider loads an [[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/orm.xml|orm.xml file]] (or other mapping file) as a resource. If you provide a mapping file, the classes and mapping information specified in the mapping file will be used. The XML mapping metadata may combine with and override annotation metadata. {{EclipseLink_Spec |
− | {{EclipseLink_Spec | + | |
|link=http://jcp.org/en/jsr/detail?id=220 | |link=http://jcp.org/en/jsr/detail?id=220 | ||
|section=Section 12.2 "XML Overriding Rules" | |section=Section 12.2 "XML Overriding Rules" | ||
− | }} | + | }} |
+ | Standard JPA 2.0 annotations are in the [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/package-summary.html javax.persistence] package. | ||
− | ==Using Metadata Annotations== | + | EclipseLink-specific JPA annotations are in the [http://www.eclipse.org/eclipselink/api/2.2/org/eclipse/persistence/annotations/package-summary.html org.eclipse.persistence.annotations package]. |
− | Use annotations to configure the persistent behavior of your entities. For example, to designate a Java class as a JPA entity, use the <tt>@Entity</tt> annotation as shown in this example: | + | |
+ | <br> | ||
+ | |||
+ | == Using Metadata Annotations == | ||
+ | |||
+ | Use annotations to configure the persistent behavior of your entities. For example, to designate a Java class as a JPA entity, use the <tt>@Entity</tt> annotation as shown in this example: | ||
<source lang="java"> | <source lang="java"> | ||
− | + | @Entity | |
− | + | public class Employee implements Serializable { | |
− | + | ... | |
− | + | } | |
− | </source> | + | </source> |
− | You can apply annotations at the following levels: | + | You can apply annotations at the following levels: |
− | *Class | + | |
− | *Method | + | *Class |
+ | *Method | ||
*Field | *Field | ||
− | EclipseLink defines a set of proprietary annotations to take advantage of EclipseLink-specific features. You can find them in the <tt>org. | + | EclipseLink defines a set of proprietary annotations to take advantage of EclipseLink-specific features. You can find them in the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/package-summary.html <tt>org.eclipse..persistence.annotations</tt>] package. These annotations are not available through the use of JPA metadata. |
+ | |||
+ | == Advantages and Disadvantages of Using Annotations == | ||
+ | |||
+ | Using annotations provides several advantages: | ||
− | |||
− | |||
*They are relatively simple to use and understand. | *They are relatively simple to use and understand. | ||
*They provide in-line metadata within with the code that it describes; you do not need to replicate the source code context of where the metadata applies. | *They provide in-line metadata within with the code that it describes; you do not need to replicate the source code context of where the metadata applies. | ||
− | |||
− | |||
+ | The primary disadvantage of annotations is that the metatdata becomes unnecessarily coupled to the code; changes to metadata require changing the source code. | ||
+ | <br> | ||
− | {{EclipseLink_JPA | + | <br> {{EclipseLink_JPA |
− | |previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/persistence.xml|persistence.xml]] | + | |previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/persistence.xml|Configuring Persistence Units Using persistence.xml ]] |
− | |next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/orm.xml|orm.xml]] | + | |next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/orm.xml|Specifying Object-Relational Mappings Using orm.xml]] |
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration|Configuration]] | |up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration|Configuration]] | ||
|version=2.2.0 DRAFT}} | |version=2.2.0 DRAFT}} |
Revision as of 08:43, 23 June 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Examples
Adding Metadata Using Annotations
An annotation is a simple, expressive means of decorating Java source code with metadata that is compiled into the corresponding Java class files for interpretation at run time by a JPA persistence provider to manage persistent behavior.
A metadata annotation represents a Java language feature that lets you attach structured and typed metadata to the source code. Annotations alone are sufficient for the metadata specification -- you do not need to use XML. Standard JPA annotations are in the javax.persistence package.For more information, see Chapter 10 "Metadata Annotations" in the JPA Specification.
For more information, see Section 12.2 "XML Overriding Rules" in the JPA Specification.
Standard JPA 2.0 annotations are in the javax.persistence package.
EclipseLink-specific JPA annotations are in the org.eclipse.persistence.annotations package.
Using Metadata Annotations
Use annotations to configure the persistent behavior of your entities. For example, to designate a Java class as a JPA entity, use the @Entity annotation as shown in this example:
@Entity public class Employee implements Serializable { ... }
You can apply annotations at the following levels:
- Class
- Method
- Field
EclipseLink defines a set of proprietary annotations to take advantage of EclipseLink-specific features. You can find them in the org.eclipse..persistence.annotations package. These annotations are not available through the use of JPA metadata.
Advantages and Disadvantages of Using Annotations
Using annotations provides several advantages:
- They are relatively simple to use and understand.
- They provide in-line metadata within with the code that it describes; you do not need to replicate the source code context of where the metadata applies.
The primary disadvantage of annotations is that the metatdata becomes unnecessarily coupled to the code; changes to metadata require changing the source code.