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.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/MappedSuperclass"
(→@MappedSuperclass) |
|||
Line 15: | Line 15: | ||
=@MappedSuperclass= | =@MappedSuperclass= | ||
− | 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. | + | 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 relatinoships 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 23: | Line 25: | ||
===@AttributeOverride=== | ===@AttributeOverride=== | ||
− | You can use the <tt>@AttributeOverride</tt> and <tt>@AttributeOverrides</tt> annotations, or <code><nowiki><attribute-override></nowiki></code> XML element to | + | You can use the <tt>@AttributeOverride</tt> and <tt>@AttributeOverrides</tt> annotations, or <code><nowiki><attribute-override></nowiki></code> 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. |
{{EclipseLink_AttributeTable | {{EclipseLink_AttributeTable | ||
Line 29: | Line 31: | ||
|content=<tr> | |content=<tr> | ||
<td>'''<tt>name</tt>'''</td> | <td>'''<tt>name</tt>'''</td> | ||
− | <td>The name of | + | <td>The name of the attribute. |
− | <td | + | <td></td> |
− | <td> | + | <td>Yes</td> |
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <td>'''<tt> | + | <td>'''<tt>column</tt>'''</td> |
− | <td>The | + | <td>The column in the subclass table. |
− | <td><code> | + | <td><code>column defined in mapped superclass</code></td> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<td>No</td> | <td>No</td> | ||
</tr> | </tr> | ||
Line 55: | Line 45: | ||
===@AssociationOverride=== | ===@AssociationOverride=== | ||
− | You can use the <tt>@AssociationOverride</tt> and <tt>@AssociationOverrides</tt> annotations, or <code><nowiki><association-override></nowiki></code> XML element to | + | You can use the <tt>@AssociationOverride</tt> and <tt>@AssociationOverrides</tt> annotations, or <code><nowiki><association-override></nowiki></code> 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. |
{{EclipseLink_AttributeTable | {{EclipseLink_AttributeTable | ||
− | |caption=@ | + | |caption=@AssociationOverrideAttributes |
|content=<tr> | |content=<tr> | ||
<td>'''<tt>name</tt>'''</td> | <td>'''<tt>name</tt>'''</td> | ||
− | <td>The name of | + | <td>The name of the attribute. |
− | <td | + | <td></td> |
− | + | <td>Yes</td> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | <td> | + | |
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <td>'''<tt> | + | <td>'''<tt>joinColumn</tt>'''</td> |
− | <td> | + | <td>The join column in the subclass table. |
− | <td> | + | <td><code>join column defined in mapped superclass</code></td> |
<td>No</td> | <td>No</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <td>'''<tt> | + | <td>'''<tt>joinTable</tt>'''</td> |
− | <td>The | + | <td>The join table for the subclass. |
− | + | <td><code>join table defined in mapped superclass</code></td> | |
<td>No</td> | <td>No</td> | ||
</tr> | </tr> |
Revision as of 14:42, 30 June 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 relatinoships 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 examples shows usages of the three different inheritance strategies for mapping an Account
hierarchy.
Example: Using SINGLE_TABLE
with @Inheritance annotation
CREATE TABLE ACCOUNT (ID NUMBER, ACCOUNT_TYPE VARCHAR(31), BALANCE NUMBER, INTERESTRATE NUMBER, RETURNCHECKS BOOLEAN)
@Entity @Table(name="ACCOUNT") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="ACCOUNT_TYPE") public abstract class Account implements Serializable { @Id private Long id; @Basic private BigDecimal balance; ... }
@Entity @DiscriminatorValue("SAVINGS") public class SavingAccount extends Account { @Basic private BigDecimal interestRate; }
@Entity @DiscriminatorValue("CHECKING") public class CheckingAccount extends Account { @Basic private boolean returnChecks; }
Example: Using SINGLE_TABLE
with <inheritance>
XML
<entity class="Account"> <table name="ACCOUNT"/> <inheritance strategy="SINGLE_TABLE"/> <discriminator-column name="ACCOUNT_TYPE"/> <attributes> <id name="id"/> <basic name="balance"/> </attributes> </entity>
<entity class="SavingAccount"> <discriminator-value>SAVINGS</discriminator-value> <attributes> <basic name="interestRate"/> </attributes> </entity>
<entity class="CheckingAccount"> <discriminator-value>CHECKING</discriminator-value> <attributes> <basic name="returnChecks"/> </attributes> </entity>