是否可以使子类(实体)从其超类(实体)覆盖OneToMany映射?目前我正在获取" HibernateException:找到对集合的共享引用",大概是因为超类实体和子类实体都链接到同一个列表。
(我的理解是@AssociationOverride只能在超类是@MappedSuperclass而不是@Entity时使用。我不想将超类从@Entity更改为@MappedSuperclass,因为我想要超类的表和能够查询它。)
如何让Hibernate映射如下所示?
@Entity
public abstract class PetOwner {
@OneToMany(targetEntity = Pet.class, mappedBy = "owner")
public <T extends Pet> List<T> getPets()
...
}
@Entity
public abstract class Pet {
@ManyToOne(targetEntity = PetOwner.class)
@JoinColumn(name = "owner_id", referencedColumnName = "id")
public <T extends PetOwner> T getOwner()
...
}
@Entity
public class CatOwner extends PetOwner {
@Override
@OneToMany(targetEntity = Cat.class, mappedBy = "owner")
public List<Cat> getPets()
...
}
@Entity
public class Cat extends Pet {
@Override
@ManyToOne(targetEntity = CatOwner.class)
@JoinColumn(name = "owner_id", referencedColumnName = "id")
public CatOwner getOwner()
...
}
答案 0 :(得分:0)
最后,我用@Override @Transient标记了子类'getters(CatOwner.getPets()和Cat.getOwner())。