【问题标题】:hibernate-JPA, ManyToOne and TablePerClass or Joined: creates wrong foreign keyhibernate-JPA、ManyToOne 和 TablePerClass 或 Joined:创建错误的外键
【发布时间】: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&lt;TestContainable&gt;public class RegistrationCompound extends Compound&lt;Batch&gt;)。

两个层次结构的 TABLE_PER_CLASS

我面临的问题是hibernate创建了一个从test_containable表到registration_compound而不是test_compound的外键约束。它似乎没有得到通用关系。

加入了两个层次结构(在我的情况下忽略缺点)

这里要求 Compound 和 Containable 是具体的类(不是抽象的) 其他类似问题。 hibernate 从可包含 -> 复合(预期)创建外键约束,但也从可包含 -> 注册复合(意外)创建外键约束。我不知道为什么要创建它?更令人困惑的是,containable 没有这样的约束 -> test_compound

总而言之,非常混乱。将尝试 single_table 但这是最​​不希望的选项...

【问题讨论】:

    标签: hibernate generics jpa foreign-keys


    【解决方案1】:

    这是由错误的映射引起的。

    @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL,
        targetEntity = Compound.class)
    private T compound;
    

    targetEntity 丢失了,由于某种原因,hibernate 只是假设实现的 1 是目标。有点烦人……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2016-09-16
      相关资源
      最近更新 更多