首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hibernate-JPA、ManyToOne和TablePerClass或Joined:创建错误的外键

hibernate-JPA、ManyToOne和TablePerClass或Joined:创建错误的外键
EN

Stack Overflow用户
提问于 2013-02-09 17:58:05
回答 1查看 734关注 0票数 1

我有两个抽象类:

代码语言:javascript
复制
@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,但这是最不需要的选项...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-17 17:05:00

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

代码语言:javascript
复制
@ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL,
    targetEntity = Compound.class)
private T compound;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14786906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档