Having a class hierarchy like so
{code:title=AbstractEntity.java|borderStyle=solid} import java.io.Serializable; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass;
@MappedSuperclass public abstract class AbstractEntity implements Serializable {
@ManyToOne(optional = false) @JoinColumn(name = "ref_id", referencedColumnName = "id", nullable = false) private ReferencedEntity realm;
protected AbstractEntity() { }
public ReferencedEntity getRef() { return ref; }
public void setRef(final ReferencedEntity ref) { this.ref = ref; } } {code}
{code:title=ReferencedEntity.java|borderStyle=solid} import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id;
@Entity public abstract class ReferencedEntity implements Serializable { @Id private int id;
public ReferencedEntity() { super(); }
public int getId() { return id; } public int setId(int id) { this.id = id; } } {code}
{code:title=ConcreteEntity.java|borderStyle=solid} import java.io.Serializable; import javax.persistence.AssociationOverride; import javax.persistence.AssociationOverrides; import javax.persistence.Entity; import javax.persistence.ForeignKey; import javax.persistence.Id; import javax.persistence.JoinColumn;
@Entity @AssociationOverrides({ @AssociationOverride(name = "ref", joinColumns = @JoinColumn(name = "ref_id"), foreignKey = @ForeignKey(name = "FK_CONCRETEENT_REF")) }) public abstract class ConcreteEntity implements Serializable { @Id private int id;
public ConcreteEntity() { super(); }
public int getId() { return id; } public int setId(int id) { this.id = id; } } {code}
The overridden foreign key for the AbstractEntity#ref property is never applied and hibernate will use the default foreign key for all derived entity classes.
Having debugged a little bit, it seems that the JPAOverriddenAnnotationReader overriding foreign key names is never used and instead the standard annotation reader from hibernate-commons-annotations is used, which does not support such overrides yet supported as overriding for example nullable does work . |
|