Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Annotations"

m (Annotations)
m (Annotations)
Line 1: Line 1:
 
=Annotations=
 
=Annotations=
 +
{{EclipseLink_API
 +
|api=*org.eclipselink.annotations
 +
*org.eclipselink.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. Annotations for object relational mapping are in the <tt>javax.persistence</tt> package.
 
+
 
+
 
+
 
+
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. Annotations for object relational mapping are in the <tt>javax.persistence</tt> package. For more information and examples, see
+
 
+
 
+
 
{{EclipseLink_Spec
 
{{EclipseLink_Spec
 
|link=http://jcp.org/en/jsr/detail?id=220  
 
|link=http://jcp.org/en/jsr/detail?id=220  
Line 21: Line 19:
  
  
==Advantages and Disadvantages of Using Annotations==
+
==Using Metadata Annotations==
Metadata annotations are relatively simple to use and understand. They provide in-line metadata located with the code that this metadata is describing–you do not need to replicate the source code context of where the metadata applies.
+
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:
  
On the other hand, annotations unnecessarily couple the metadata to the code. Thus, changes to metadata require changing the source code.
+
<source lang="java">
 +
@Entity
 +
public class Employee implements Serializable {
 +
    ...
 +
}
 +
</source>
 +
 
 +
{{EclipseLink_Spec
 +
|link=http://jcp.org/en/jsr/detail?id=220
 +
|section=Chapter 8 "Metadata annotations" and Section 8.1 "Entity"}}
 +
 
 +
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 <tt>org.eclipselink.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 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.
  
  

Revision as of 13:33, 16 June 2010

Annotations

Elug api package icon.png Key API {{{apis}}}

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. Annotations for object relational mapping are in the javax.persistence package.

Elug javaspec icon.gif

For more information, see Chapter 8 "Metadata Annotations" in the JPA Specification.

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 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.

Elug javaspec icon.gif

For more information, see Section 10.1 "XML Overriding Rules" in the JPA Specification.


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 {
     ...
 }
Elug javaspec icon.gif

For more information, see Chapter 8 "Metadata annotations" and Section 8.1 "Entity" in the JPA Specification.

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.eclipselink.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.



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