Jump to: navigation, search

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

(Example: Using @MappedSuperClass annotation)
Line 21: Line 21:
 
Embeddables do not currently support the <tt>@Inheritance</tt> annotation, but inheritance is support in EclipseLink through usage of a <tt>DescriptorCustomizer</tt>.
 
Embeddables do not currently support the <tt>@Inheritance</tt> annotation, but inheritance is support in EclipseLink through usage of a <tt>DescriptorCustomizer</tt>.
  
An Embbeddable cannot define a <tt>@Table</tt> annotation, and the columns used in its mappings are be default the names of the column is its parent's table.  If the embeddable class is shared with multiple parents, then each parent can override the column names using the <tt>@AttributeOverride</tt> and <tt>@AssociationOverride</tt> annotations.
+
An embeddable cannot define a <tt>@Table</tt> annotation, and the columns used in its mappings are be default the names of the column is its parent's table.  If the embeddable class is shared with multiple parents, then each parent can override the column names using the <tt>@AttributeOverride</tt> and <tt>@AssociationOverride</tt> annotations.
  
 
The <tt>@Embeddable</tt> annotation does not have any attributes.
 
The <tt>@Embeddable</tt> annotation does not have any attributes.
Line 96: Line 96:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
 +
@Table(name="EMP")
 
public class Employee implements Serializable {
 
public class Employee implements Serializable {
 
     @Id
 
     @Id
Line 107: Line 108:
 
</source>
 
</source>
  
======''Example: Using <code><nowiki><mapped-superclass></nowiki></code> XML''======
+
======''Example: Using <code><nowiki><embeddable></nowiki></code> XML''======
  
 
<source lang="xml">
 
<source lang="xml">
<mapped-superclass class="PersistentObject">
 
    <attributes>
 
        <id name="id"/>
 
        <version name="version"/>
 
    </attributes>
 
</mapped-superclass>
 
 
<entity class="Employee">
 
<entity class="Employee">
 
     <table name="EMP">
 
     <table name="EMP">
 
     <attributes>
 
     <attributes>
 +
        <id name="id"/>
 
         <basic name="name"/>
 
         <basic name="name"/>
 +
        <embeddable name="status"/>
 
         ...
 
         ...
 
     </attributes>
 
     </attributes>
 
</entity>
 
</entity>
<entity class="Project">
+
<embeddable class="EmployeementStatus">
    <table name="PROJECT">
+
    <attribute-override name="id">
+
        <column name="PROJ_ID"/>
+
    </attribute-override>
+
 
     <attributes>
 
     <attributes>
         ...
+
         <basic name="status"/>
 +
        <basic name="level"/>
 +
        <basic name="startDate"/>
 +
        <basic name="endDate"/>
 
     </attributes>
 
     </attributes>
</entity>
+
</embeddable >
 
</source>
 
</source>
 
<br>
 
<br>

Revision as of 11:43, 28 July 2011

EclipseLink JPA


@Embeddable

You can use the @Embeddable annotation or <embeddable> XML element to map an embedded class. An embeddable is a special type of class that is not directly persistent, but persisted only with its parent entity. An embeddable has no persistent identity, and no Id mapping. An embeddable can contain any type of attribute mapping including @Basic, @OneToOne, @ManyToOne, @OneToMany, @ManyToMany, @ElementCollection or another @Embedded. Embeddables cannot contain an @Id mapping.

An embeddable can be used to define an entities Id, normally in the case of a composite id. The id attributes in the embeddable will be mapped as @Basic not as @Id, as all attributes are required to be part of the @Id. The parent entity will use the @EmbeddedId to map the embeddeable instead of the @Embedded mapping.

An embeddable can be referenced from an entity or another embeddable using the @Embedded annotation for a single reference, or the @ElementCollection annotation for a Collection or Map reference. An embeddable can also be used in any Map key using the @MapKeyClass annotation.

Embeddables do not currently support the @Inheritance annotation, but inheritance is support in EclipseLink through usage of a DescriptorCustomizer.

An embeddable cannot define a @Table annotation, and the columns used in its mappings are be default the names of the column is its parent's table. If the embeddable class is shared with multiple parents, then each parent can override the column names using the @AttributeOverride and @AssociationOverride annotations.

The @Embeddable annotation does not have any attributes.

Elug javaspec icon.gif

For more information, see Section 2.5 "Embeddable Classes" in the JPA Specification.

Elug javaspec icon.gif

For more information, see Section 11.1.13 "Embeddable Annotation" in the JPA Specification.

@AttributeOverride

You can use the @AttributeOverride and @AttributeOverrides annotations, or <attribute-override> XML element to override the column for a basic attribute in an embedded relationship mapping. This allows for the column name to be different if the embeddable class is used by more than entity or if an entity defines multiple embedded relationships.

@AttributeOverride Attributes
Attribute Description Default Required?
name The name of the attribute. Yes
column The column in the source table for the embedded relationship. column defined in embeddable No
Elug javaspec icon.gif

For more information, see Section 11.1.4 "AttributeOverride Annotation" in the JPA Specification.

@AssociationOverride

You can use the @AssociationOverride and @AssociationOverrides annotations, or <association-override> XML element to override the join column or join table for a relationship attribute in an embedded relationship mapping. This allows for the join column name to be different if the embeddable class is used by more than entity or if an entity defines multiple embedded relationships.

@AssociationOverrideAttributes
Attribute Description Default Required?
name The name of the attribute. Yes
joinColumn The join column in the source table for the embedded relationship. join column defined in embeddable No
joinTable The join table for the subclass. join table defined in embeddable No
Elug javaspec icon.gif

For more information, see Section 11.1.2 "AssociationOverride Annotation" in the JPA Specification.

The following example shows usage of an embbeddable.

Example: Using @Embeddable annotation
@Embeddable 
public class EmployeementStatus implements Serializable {
    @Basic
    private String status;
    @Basic
    private String level;
    @Basic
    private java.sql.Date startDate;
    @Basic
    private java.sql.Date endDate;
    ...
}
@Entity
@Table(name="EMP")
public class Employee implements Serializable {
    @Id
    private Long id;
    @Basic
    private String name;
    @Embedded
    private EmployeementStatus status;
    ...
}
Example: Using <embeddable> XML
<entity class="Employee">
    <table name="EMP">
    <attributes>
        <id name="id"/>
        <basic name="name"/>
        <embeddable name="status"/>
        ...
    </attributes>
</entity>
<embeddable class="EmployeementStatus">
    <attributes>
        <basic name="status"/>
        <basic name="level"/>
        <basic name="startDate"/>
        <basic name="endDate"/>
    </attributes>
</embeddable >


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