This is quite different to discriminated inheritance where the discriminator is stored along with the referenced entity hierarchy.
For example, consider an Order entity containing Payment information,
where a Payment might be a CashPayment or a CreditCardPayment.
An @Any mapping would store the discriminator value identifying the concrete
type of Payment along with the state of the associated Order, instead
of storing it with the Payment 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 is not be declared as an entity type, and
is not annotated @Entity. It might even be an
interface, or at most just a mapped
superclass, of CashPayment and CreditCardPayment. So in terms of the
object/relational mappings, CashPayment and CreditCardPayment would
not be considered to participate in the same entity inheritance hierarchy.
On the other hand, CashPayment and CreditCardPayment must have the
same identifier type.
It's reasonable to think of the "foreign key" in an Any 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, or JdbcTypeCode specifies
the type of the discriminator.
AnyDiscriminatorValue specifies how discriminator values map to entity
types.
jakarta.persistence.Column or Formula specifies the column or
formula in which the discriminator value is stored.
AnyKeyJavaType, AnyKeyJavaClass, AnyKeyJdbcType, or
AnyKeyJdbcTypeCode specifies the type of the foreign key.
jakarta.persistence.JoinColumn specifies 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.
ManyToAny
| Modifier and Type | Method and Description |
|---|---|
| public FetchType | |
| public boolean |
| fetch | back to summary |
|---|---|
| public FetchType fetch() Specifies whether the value of the field or property may be lazily loaded or must be eagerly fetched:
If not explicitly specified, the default is | |
| optional | back to summary |
|---|---|
| public boolean optional() Whether the association is optional.
If the discriminator
| |