Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings"

From Eclipsepedia

Jump to: navigation, search
m (Basic Mappings)
 
(22 intermediate revisions by 3 users 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/Basic.html @Basic]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Enumerated.html @Enumerated]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Temporal.html @Temporal]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Lob.html @Lob]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Transient.html @Transient]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Column.html @Column]
 +
|nativeapi=y
 +
|nativeapis=
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/mappings/DirectToFieldMapping.html DirectToFieldMapping]
 +
}}
 +
 
=Basic Mappings=
 
=Basic Mappings=
 
Simple Java types are mapped as part of the immediate state of an entity in its fields or properties. Mappings of simple Java types are called ''basic mappings''.
 
Simple Java types are mapped as part of the immediate state of an entity in its fields or properties. Mappings of simple Java types are called ''basic mappings''.
Line 5: Line 23:
  
 
Use the following annotations to fine-tune how your database implements these mappings:
 
Use the following annotations to fine-tune how your database implements these mappings:
* [[#@Basic|@Basic]]
+
* [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Basic|@Basic]]
* @Enumerated
+
* [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Enumerated|@Enumerated]]
* @Temporal
+
* [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Temporal|@Temporal]]
* @Lob
+
* [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Lob|@Lob]]
* @Transient
+
* [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Transient |@Transient]]
For additional mapping information, see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters|Default Conversions and Converters]]
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Column|@Column]]  
 
+
*[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Lazy Basics|Lazy Basics]]
 
+
 
+
==@Basic==
+
By default, EclipseLink persistence provider automatically configures <tt>@Basic</tt> mapping for most Java primitive types, wrappers of the primitive types, and enumerated types.
+
 
+
EclipseLink uses the default column name format of <tt><field-name></tt> or <property-name> in uppercase characters.
+
 
+
You may explicitly place an optional <tt>@Basic</tt> annotation on a field or property to explicitly mark it as persistent.
+
 
+
{{EclipseLink_Note
+
|note=The <tt>@Basic</tt> annotation is mostly for documentation purposes – it is not required for the field or property to be persistent.
+
}}
+
 
+
 
+
Use the <tt>@Basic</tt> annotation to do the following:
+
* configure the fetch type to <tt>LAZY</tt><nowiki>;</nowiki>
+
* configure the mapping to forbid null values (for nonprimitive types) in case null values are inappropriate for your application.
+
 
+
The <tt>@Basic</tt> annotation has the following attributes:
+
* <tt>fetch</tt> – By default, EclipseLink persistence provider uses a fetch type of <tt>javax.persitence.FetchType.EAGER</tt><nowiki>: data must be eagerly fetched.</nowiki>If the default is inappropriate for your application or a particular persistent field, set <tt>fetch</tt> to <tt>FetchType.LAZY</tt><nowiki>: this is a hint to the persistence provider that data should be fetched lazily when it is first accessed (if possible).</nowiki>You are not required to specify the value of this attribute.<br>For more information, see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#What You May Need to Know About EclipseLink JPA Lazy Loading|What You May Need to Know About EclipseLink JPA Lazy Loading]].
+
* <tt>optional</tt> – By default, EclipseLink persistence provider assumes that the value of all (nonprimitive) fields and properties may be <tt>null</tt>.<br>If the default is inappropriate for your application, set this the value of this attribute to <tt>false</tt>.<br><br>You are not required to specify the value of this attribute.
+
 
+
This example shows how to use this annotation to specify a fetch type of <tt>LAZY</tt> for a basic mapping.
+
 
+
 
+
<span id="Example 18-11"></span>
+
''''' Usage of the @Basic Annotation'''''
+
<source lang="java">
+
@Entity
+
public class Employee implements Serializable {
+
    ...
+
    @Basic(fetch=LAZY)
+
    protected String getName() {
+
        return name;
+
    }
+
    ...
+
}
+
</source>
+
  
For more information and examples, see Section 9.1.18 "Basic Annotation" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification].
 
  
For more information on EclipseLink direct mappings and relationship mappings, see [[Introduction%20to%20Relational%20Mappings%20(ELUG)#Relational Mapping Types|Relational Mapping Types]].
+
For all mapping types there are a common set of options:
 +
* '''Read-Only''': Specifies that the mapping should populate the value on read and copy. Required when multiple mappings share the same database column.
 +
* '''Converters''': Allows custom data types and data conversions to be used with most mapping types
 +
** Annotations: <tt>@Converter</tt>, <tt>@TypeConverter</tt>, <tt>@ObjectTypeConverter</tt>, <tt>@StructConverter</tt>, <tt>@Convert</tt>
 +
** XML: <tt><converter></tt>, <tt><type-converter></tt>, <tt><object-type-converter></tt>, <tt><struct-converter></tt>, <tt><convert></tt>
 +
<br />See [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters|Default Conversions and Converters]] for details.
  
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|Id]]
+
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Configuring Mapping]]
|next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Column|Column]]
+
|next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Basic|@Basic]]
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Mapping]]}}
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Configuring Mapping]]
 +
|version=2.2.0 DRAFT}}

Latest revision as of 11:31, 27 October 2011

EclipseLink JPA


[edit] Basic Mappings

Simple Java types are mapped as part of the immediate state of an entity in its fields or properties. Mappings of simple Java types are called basic mappings.

By default, EclipseLink persistence provider automatically configures a basic mapping for simple types.

Use the following annotations to fine-tune how your database implements these mappings:


For all mapping types there are a common set of options:

  • Read-Only: Specifies that the mapping should populate the value on read and copy. Required when multiple mappings share the same database column.
  • Converters: Allows custom data types and data conversions to be used with most mapping types
    • Annotations: @Converter, @TypeConverter, @ObjectTypeConverter, @StructConverter, @Convert
    • XML: <converter>, <type-converter>, <object-type-converter>, <struct-converter>, <convert>


See Default Conversions and Converters for details.


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