Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/MappedSuperclass"
(→@MappedSuperclass) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
You can use the <tt>@MappedSuperclass</tt> annotation or <code><nowiki><mapped-superclass></nowiki></code> XML element to define mappings for an abstract or non-persistent superclass, that are inherited by its subclass entities. 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 attribute. A mapped superclass should normally be abstract but is not required to be, but cannot have any persistent instances. | You can use the <tt>@MappedSuperclass</tt> annotation or <code><nowiki><mapped-superclass></nowiki></code> XML element to define mappings for an abstract or non-persistent superclass, that are inherited by its subclass entities. 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 attribute. A mapped superclass should normally be abstract but is not required to be, but cannot have any persistent instances. | ||
− | Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior. Entities cannot have | + | Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior. Entities cannot have relationships to mapped superclasses, and mapped superclasses cannot be queried. |
The <tt>@MappedSuperclass</tt> annotation does not have any attributes. | The <tt>@MappedSuperclass</tt> annotation does not have any attributes. | ||
Line 70: | Line 70: | ||
{{EclipseLink_Spec|section=Section 11.1.2 "AssociationOverride Annotation"}} | {{EclipseLink_Spec|section=Section 11.1.2 "AssociationOverride Annotation"}} | ||
− | The following | + | The following example shows usages of a generic persistence mapped superclass. |
− | <span id="Example: @ | + | <span id="Example: @MappedSuperClass"></span> |
− | ======''Example: Using | + | ======''Example: Using @MappedSuperClass annotation''====== |
− | + | ||
− | + | ||
− | + | ||
<source lang="java"> | <source lang="java"> | ||
− | @ | + | @MappedSuperclass |
− | + | public abstract class PersistentObject implements Serializable { | |
− | + | ||
− | + | ||
− | public abstract class | + | |
@Id | @Id | ||
private Long id; | private Long id; | ||
− | @ | + | @Version |
− | private | + | private Long version; |
... | ... | ||
} | } | ||
Line 94: | Line 88: | ||
<source lang="java"> | <source lang="java"> | ||
@Entity | @Entity | ||
− | @ | + | @Table("EMP") |
− | public class | + | public class Employee extends PersistentObject { |
@Basic | @Basic | ||
− | private | + | private String name; |
+ | ... | ||
} | } | ||
</source> | </source> | ||
Line 103: | Line 98: | ||
<source lang="java"> | <source lang="java"> | ||
@Entity | @Entity | ||
− | @ | + | @Table("PROJECT") |
− | public class | + | @AttributeOverride(name="id" column=@Column(name="PROJ_ID")) |
+ | public class Project extends PersistentObject { | ||
@Basic | @Basic | ||
− | private | + | private String name; |
+ | ... | ||
} | } | ||
</source> | </source> | ||
− | ======''Example: Using | + | ======''Example: Using <code><nowiki><mapped-superclass></nowiki></code> XML''====== |
<source lang="xml"> | <source lang="xml"> | ||
− | < | + | <mapped-superclass class="PersistentObject"> |
− | + | ||
− | + | ||
− | + | ||
<attributes> | <attributes> | ||
<id name="id"/> | <id name="id"/> | ||
− | < | + | <version name="version"/> |
</attributes> | </attributes> | ||
− | </ | + | </mapped-superclass> |
− | < | + | <entity class="Employee"> |
− | + | <table name="EMP"> | |
− | + | ||
− | < | + | |
− | + | ||
<attributes> | <attributes> | ||
− | <basic name=" | + | <basic name="name"/> |
+ | ... | ||
</attributes> | </attributes> | ||
</entity> | </entity> | ||
− | < | + | <entity class="Project"> |
− | + | <table name="PROJECT"> | |
− | < | + | <attribute-override name="id"> |
− | < | + | <column name="PROJ_ID"/> |
− | + | </attribute-override> | |
<attributes> | <attributes> | ||
− | + | ... | |
</attributes> | </attributes> | ||
</entity> | </entity> | ||
Line 145: | Line 137: | ||
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Inheritance|@Inheritance]] | |previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Inheritance|@Inheritance]] | ||
− | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/ | + | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Embeddable|@Embeddable]] |
|up= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]] | |up= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]] | ||
|version=2.2.0 DRAFT}} | |version=2.2.0 DRAFT}} |
Latest revision as of 11:53, 28 July 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Examples
@MappedSuperclass
You can use the @MappedSuperclass annotation or <mapped-superclass>
XML element to define mappings for an abstract or non-persistent superclass, that are inherited by its subclass entities. 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 attribute. A mapped superclass should normally be abstract but is not required to be, but cannot have any persistent instances.
Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior. Entities cannot have relationships to mapped superclasses, and mapped superclasses cannot be queried.
The @MappedSuperclass annotation does not have any attributes.
For more information, see Section 2.1.9 "Inheritance" in the JPA Specification.
For more information, see Section 11.1.34 "MappedSuperclass 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 a mapped superclass. This allows for the column name to be different in each subclass.
Attribute | Description | Default | Required? |
---|---|---|---|
name | The name of the attribute. | Yes | |
column | The column in the subclass table. | column defined in mapped superclass |
No |
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 a mapped superclass. This allows for the join column name or join table to be different in each subclass.
Attribute | Description | Default | Required? |
---|---|---|---|
name | The name of the attribute. | Yes | |
joinColumn | The join column in the subclass table. | join column defined in mapped superclass |
No |
joinTable | The join table for the subclass. | join table defined in mapped superclass |
No |
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>