【发布时间】:2013-01-25 01:33:48
【问题描述】:
我有这两个抽象类:
@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的外键约束。它似乎没有得到通用关系。
加入了两个层次结构(在我的情况下忽略缺点)
这里要求 Compound 和 Containable 是具体的类(不是抽象的) 其他类似问题。 hibernate 从可包含 -> 复合(预期)创建外键约束,但也从可包含 -> 注册复合(意外)创建外键约束。我不知道为什么要创建它?更令人困惑的是,containable 没有这样的约束 -> test_compound
总而言之,非常混乱。将尝试 single_table 但这是最不希望的选项...
【问题讨论】:
标签: hibernate generics jpa foreign-keys