Per mappare più entità su una sola colonna, Hibernate fornisce l’annotazione @Any:
@Any(metaColumn = @Column(name = "userType"))
@AnyMetaDef(
name = "PropertyMetaDef",
metaType = "string",
idType = "long",
metaValues = {
@MetaValue(value = "User", targetEntity = User.class),
@MetaValue(value = "LDAP", targetEntity = LDAPUser.class)
}
)
@JoinColumn(name="assignedto_id", referencedColumnName="id", insertable=false, updatable=false)
private Object assignedTo;Lo svantaggio è che non vengono creati foreign key reali.
In alternativa, se le classi possono estendere una superclasse comune, puoi usare una mappatura ereditaria InheritanceType.JOINED per ottenere un effetto simile.