我有两个抽象类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="compound")
public abstract class Compound<T extends Containable {
@OneToMany(fetch = FetchType.EAGER, mappedBy="compound",
targetEntity=Containable.class, cascade = CascadeType.ALL)
private Set<T> containables = new HashSet<>();
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name="containable")
public abstract class Containable<T extends Compound> {
@ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private T compound;
}在我的测试中,每一个都有两个实现,TestCompound和TestContainable作为一对,RegistrationCompound和Batch作为另一对。(例如,public class TesCompound extends Compound<TestContainable>和public class RegistrationCompound extends Compound<Batch>)。
两个层次结构的TABLE_PER_CLASS
我面临的问题是hibernate创建了一个从test_containable表到registration_compound而不是test_compound的外键约束。它似乎没有得到一般的关系。
为两个层次结构连接(在我的例子中忽略缺点)
这里要求复合和可容纳是具体的类(不是抽象的),或者类似的问题。hibernate从可包含的->复合(expected)创建外键约束,但也从containable -> registration_compound (Expected)创建外键约束。我不知道为什么要创建这个?更令人困惑的是,可包含的-> test_compound没有这样的约束。
总而言之,这非常令人困惑。我将尝试single_table,但这是最不需要的选项...
发布于 2013-02-17 17:05:00
这是由错误的映射引起的。
@ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL,
targetEntity = Compound.class)
private T compound;targetEntity丢失了,出于某种原因,hibernate只是假设实现中的1个是目标。有点烦人。
https://stackoverflow.com/questions/14786906
复制相似问题