Having a class hierarchy like so
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 Realm getRef()
{
return ref;
}
public void setRef(final ReferencedEntity ref)
{
this.ref = ref;
}
}
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; }
}
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 = "realm",
joinColumns = @JoinColumn(name = "realm_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; }
}
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 is never used and instead the standard annotation reader from hibernate-commons-annotations is used, which does not support such overrides. |