Annotation Interface Any
This is quite different todiscriminated inheritance where the discriminator is stored along with the referenced entity hierarchy.
For example, consider anOrder entity containingPayment information, where aPayment might be aCashPayment or aCreditCardPayment. An@Any mapping would store the discriminator value identifying the concrete type ofPayment along with the state of the associatedOrder, instead of storing it with thePayment entity itself.
interface Payment { ... } @Entity class CashPayment { ... } @Entity class CreditCardPayment { ... } @Entity class Order { ... @Any @AnyKeyJavaClass(UUID.class) //the foreign key type @JoinColumn(name="payment_id") //the foreign key column @Column(name="payment_type") //the discriminator column @AnyDiscriminatorValue(discriminator="CASH", entity=CashPayment.class) @AnyDiscriminatorValue(discriminator="CREDIT", entity=CreditCardPayment.class) Payment payment; ... } In this example,Payment isnot be declared as an entity type, and is not annotated@Entity. It might even be an interface, or at most just amapped superclass, ofCashPayment andCreditCardPayment. So in terms of the object/relational mappings,CashPayment andCreditCardPayment wouldnot be considered to participate in the same entity inheritance hierarchy. On the other hand,CashPayment andCreditCardPayment must have the same identifier type.
It's reasonable to think of the "foreign key" in anAny mapping is really a composite value made up of the foreign key and discriminator taken together. Note, however, that this composite foreign key is only conceptual and cannot be declared as a physical constraint on the relational database table.
AnyDiscriminator,JdbcType, orJdbcTypeCodespecifies the type of the discriminator.AnyDiscriminatorValuespecifies how discriminator values map to entity types.ColumnorFormulaspecifies the column or formula in which the discriminator value is stored.AnyKeyJavaType,AnyKeyJavaClass,AnyKeyJdbcType, orAnyKeyJdbcTypeCodespecifies the type of the foreign key.JoinColumnspecifies the foreign key column.
Of course,Any mappings are disfavored, except in extremely special cases, since it's much more difficult to enforce referential integrity at the database level.
- See Also:
Optional Element Summary
Optional Elements
Element Details
fetch
FetchType fetchSpecifies whether the value of the field or property may be lazily loaded or must be eagerly fetched:EAGERspecifies that the association must be eagerly fetched.LAZYallows the association to be fetched lazily, but this is possible only when bytecode enhancement is used.
If not explicitly specified, the default is
EAGER.- Default:
- EAGER
optional
boolean optionalWhether the association is optional.If the discriminator
Columnor theJoinColumnare not nullable the association is always considered non-optional, regardless of this value.- Returns:
falseif the association cannot be null.
- Default:
- true