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

From Eclipsepedia

Jump to: navigation, search
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.   
 
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.   
  
 +
The <tt>@MappedSuperclass</tt> annotation does not have any attributes.
  
 
{{EclipseLink_Spec|section=Section 2.1.9 "Inheritance"}}
 
{{EclipseLink_Spec|section=Section 2.1.9 "Inheritance"}}
{{EclipseLink_Spec|section=Section 9.1.x "MappedSuperclassAnnotation"}}
+
{{EclipseLink_Spec|section=Section 11.1.34 "MappedSuperclass Annotation"}}
  
 
===@AttributeOverride===
 
===@AttributeOverride===
You can use the <tt>@DiscriminatorColumn</tt> annotation or <code><nowiki><discriminator-column></nowiki></code> XML element to configure the name or type of the inheritance discriminator column.  The discriminator column is required for <code>SINGLE_TABLE</code> and <code>JOINED</code> inheritance and stores the associated entity type for the row.  The default name for the discriminator column is <code>DTYPE</code>.  JPA only allows String or Integer values for discriminators.  Through the EclipseLink API, it is possible to use other discriminator types, and it is possible to not have a discriminator, or use custom discriminator, see [[#Advanced Inheritance Configuration|Advanced Inheritance Configuration]].
+
You can use the <tt>@AttributeOverride</tt> and <tt>@AttributeOverrides</tt> annotations, or <code><nowiki><attribute-override></nowiki></code> XML element to configure the name or type of the inheritance discriminator column.  The discriminator column is required for <code>SINGLE_TABLE</code> and <code>JOINED</code> inheritance and stores the associated entity type for the row.  The default name for the discriminator column is <code>DTYPE</code>.  JPA only allows String or Integer values for discriminators.  Through the EclipseLink API, it is possible to use other discriminator types, and it is possible to not have a discriminator, or use custom discriminator, see [[#Advanced Inheritance Configuration|Advanced Inheritance Configuration]].
  
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable
|caption=@DiscriminatorColumn Attributes
+
|caption=@AttributeOverride Attributes
 
|content=<tr>
 
|content=<tr>
 
  <td>'''<tt>name</tt>'''</td>
 
  <td>'''<tt>name</tt>'''</td>
Line 51: Line 52:
 
</tr>
 
</tr>
 
}}
 
}}
{{EclipseLink_Spec|section=Section 11.1.10 "DiscriminatorColumn Annotation"}}
+
{{EclipseLink_Spec|section=Section 11.1.4 "AttributeOverride Annotation"}}
  
 
===@AssociationOverride===
 
===@AssociationOverride===
You can use the <tt>@DiscriminatorValue</tt> annotation or <code><nowiki><discriminator-value></nowiki></code> XML element to configure the value of the inheritance discriminator.  The discriminator value can be specified in each non-abstract class in the inheritance hierarchyBy default the discriminator value is the entity's name, which defaults to its unprefixed class name.  The discriminator value is always specified as a String, but is converted to the discriminator column type.
+
You can use the <tt>@AssociationOverride</tt> and <tt>@AssociationOverrides</tt> annotations, or <code><nowiki><association-override></nowiki></code> XML element to configure the name or type of the inheritance discriminator column.  The discriminator column is required for <code>SINGLE_TABLE</code> and <code>JOINED</code> inheritance and stores the associated entity type for the rowThe default name for the discriminator column is <code>DTYPE</code>.  JPA only allows String or Integer values for discriminators.  Through the EclipseLink API, it is possible to use other discriminator types, and it is possible to not have a discriminator, or use custom discriminator, see [[#Advanced Inheritance Configuration|Advanced Inheritance Configuration]].
  
 +
{{EclipseLink_AttributeTable
 +
|caption=@AssociationOverride Attributes
 +
|content=<tr>
 +
<td>'''<tt>name</tt>'''</td>
 +
<td>The name of column to be used to store the class discriminator value.
 +
<td><code>DTYPE</code></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>discriminatorType</tt>'''</td>
 +
<td>The type of the discriminator value, defined in <code>DiscriminatorType</code>, one of <code>STRING</code>, <code>INTEGER</code>, and <code>CHAR</code>.
 +
<td><code>STRING</code></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>columnDefinition</tt>'''</td>
 +
<td>Optional column description for use with DDL generation.</td>
 +
<td>generated base on <tt>discriminatorType</tt></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>length</tt>'''</td>
 +
<td>The size of the column for DDL generation.  Only relevant for <code>STRING</code> types.</td>
 +
<td>31</td>
 +
<td>No</td>
 +
</tr>
 +
}}
 +
{{EclipseLink_Spec|section=Section 11.1.2 "AssociationOverride Annotation"}}
  
 
The following examples shows usages of the three different inheritance strategies for mapping an <code>Account</code> hierarchy.
 
The following examples shows usages of the three different inheritance strategies for mapping an <code>Account</code> hierarchy.

Revision as of 14:17, 30 June 2011

EclipseLink JPA


@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.

The @MappedSuperclass annotation does not have any attributes.

Elug javaspec icon.gif

For more information, see Section 2.1.9 "Inheritance" in the JPA Specification.

Elug javaspec icon.gif

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 configure the name or type of the inheritance discriminator column. The discriminator column is required for SINGLE_TABLE and JOINED inheritance and stores the associated entity type for the row. The default name for the discriminator column is DTYPE. JPA only allows String or Integer values for discriminators. Through the EclipseLink API, it is possible to use other discriminator types, and it is possible to not have a discriminator, or use custom discriminator, see Advanced Inheritance Configuration.

@AttributeOverride Attributes
Attribute Description Default Required?
name The name of column to be used to store the class discriminator value. DTYPE No
discriminatorType The type of the discriminator value, defined in DiscriminatorType, one of STRING, INTEGER, and CHAR. STRING No
columnDefinition Optional column description for use with DDL generation. generated base on discriminatorType No
length The size of the column for DDL generation. Only relevant for STRING types. 31 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 configure the name or type of the inheritance discriminator column. The discriminator column is required for SINGLE_TABLE and JOINED inheritance and stores the associated entity type for the row. The default name for the discriminator column is DTYPE. JPA only allows String or Integer values for discriminators. Through the EclipseLink API, it is possible to use other discriminator types, and it is possible to not have a discriminator, or use custom discriminator, see Advanced Inheritance Configuration.

@AssociationOverride Attributes
Attribute Description Default Required?
name The name of column to be used to store the class discriminator value. DTYPE No
discriminatorType The type of the discriminator value, defined in DiscriminatorType, one of STRING, INTEGER, and CHAR. STRING No
columnDefinition Optional column description for use with DDL generation. generated base on discriminatorType No
length The size of the column for DDL generation. Only relevant for STRING types. 31 No
Elug javaspec icon.gif

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>


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