Skip to main content

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.

Jump to: navigation, search

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

(New page: {{EclipseLink_UserGuide |info=y |toc=n |eclipselink=y |eclipselinktype=JPA |api=y |apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Embeddable.html @Embeddable] * [...)
 
Line 13: Line 13:
  
 
=@Embeddable=
 
=@Embeddable=
You can use the <tt>@Embeddable</tt> annotation or <code><nowiki><embeddable></nowiki></code> XML element to map an embedded class.  A mapped superclass is a special type of class that is not persistent itself, but has subclasses that are persistent.  A mapped superclass is useful for defined a common persistence superclass that defines common behavior across a set of classes, such as an id or version attributeA mapped superclass should normally be abstract but is not required to be, but cannot have any persistent instances.
+
You can use the <tt>@Embeddable</tt> annotation or <code><nowiki><embeddable></nowiki></code> 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 <tt>Id</tt> mappingAn embeddable can contain any type of attribute mapping including <tt>@Basic</tt>, <tt>@OneToOne</tt>, <tt>@ManyToOne</tt>, <tt>@OneToMany</tt>, <tt>@ManyToMany</tt>, <tt>@ElementCollection</tt> or another <tt>@Embedded</tt>Embeddables cannot contain an <tt>@Id</tt> mapping.
  
Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behaviorEntities cannot have relatinoships to mapped superclasses, and mapped superclasses cannot be queried.
+
An embeddable can be used to define an entities <tt>Id</tt>, normally in the case of a composite id.  The id attributes in the embeddable will be mapped as <tt>@Basic</tt> not as <tt>@Id</tt>, as all attributes are required to be part of the <tt>@Id</tt>The parent entity will use the <tt>@EmbeddedId</tt> to map the embeddeable instead of the <tt>@Embedded</tt> mapping.
 +
 
 +
An embeddable can be referenced from an entity or another embeddable using the <tt>@Embedded</tt> annotation for a single reference, or the <tt>@ElementCollection</tt> annotation for a <tt>Collection</tt> or <tt>Map</tt> reference.  An embeddable can also be used in any <tt>Map</tt> key using the <tt>@MapKeyClass</tt> annotation.
 +
 
 +
Embeddables do not currently support the <tt>@Inheritance</tt> annotation, but inheritance is support in EclipseLink through usage of a <tt>DescriptorCustomizer</tt>.
  
 
The <tt>@Embeddable</tt> annotation does not have any attributes.
 
The <tt>@Embeddable</tt> annotation does not have any attributes.
  
{{EclipseLink_Spec|section=Section 2.1.9 "Inheritance"}}
+
{{EclipseLink_Spec|section=Section 2.5 "Embeddable Classes"}}
{{EclipseLink_Spec|section=Section 11.1.34 "MappedSuperclass Annotation"}}
+
{{EclipseLink_Spec|section=Section 11.1.13 "Embeddable Annotation"}}
  
 
===@AttributeOverride===
 
===@AttributeOverride===

Revision as of 11:15, 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.

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 mapped superclass No
joinTable The join table for the subclass. join table defined in mapped superclass No
Elug javaspec icon.gif

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

The following example shows usages of a generic persistence mapped superclass.

Example: Using @MappedSuperClass annotation
@MappedSuperclass
public abstract class PersistentObject implements Serializable {
    @Id
    private Long id;
    @Version
    private Long version;
    ...
}
@Entity
@Table("EMP")
public class Employee extends PersistentObject {
    @Basic
    private String name;
    ...
}
@Entity
@Table("PROJECT")
@AttributeOverride(name="id" column=@Column(name="PROJ_ID"))
public class Project extends PersistentObject {
    @Basic
    private String name;
    ...
}
Example: Using <mapped-superclass> XML
<mapped-superclass class="PersistentObject">
    <attributes>
        <id name="id"/>
        <version name="version"/>
    </attributes>
</mapped-superclass>
<entity class="Employee">
    <table name="EMP">
    <attributes>
        <basic name="name"/>
        ...
    </attributes>
</entity>
<entity class="Project">
    <table name="PROJECT">
    <attribute-override name="id">
        <column name="PROJ_ID"/>
    </attribute-override>
    <attributes>
        ...
    </attributes>
</entity>


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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.