繁体   English   中英

双向一对多继承不起作用(带有休眠3.4.5的jpa)

[英]Bi-directional one-to-many with inheritance not working (jpa with hibernate 3.5.4)

我正在尝试绘制双向一对多关系。 我遇到了麻烦,因为“许多”方面引用了抽象超类。 在互联网上寻找可能的原因时,我发现这是一个已知问题,但无法为我的案子找到解决方案。

我已经检查了博客上的变通方法,“单表,没有mappingBy”看起来像是一种解决方案,但我确实需要双向关联。

这些是我要映射的类:

所属方

@Entity(name = "CC_Incl_Site")
public class IncludedSite {
    @OneToMany(fetch=FetchType.LAZY, mappedBy = "includedSite")
    private Set<CtaContractBase> ctas = new HashSet<CtaContractBase>();

    @OneToMany(fetch=FetchType.LAZY, mappedBy = "includedSite")
    private Set<WoContractBase> wos = new HashSet<WoContractBase>();  
}

另一边:

@Entity
public abstract class SCContract extends Contract {

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "incl_site_id")
    private IncludedSite includedSite;
}

合同(SCContract的超类):

@Entity(name = "CC_CONTRACT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "contractType", discriminatorType = DiscriminatorType.STRING)
@ForceDiscriminator
public abstract class Contract {

...

}

尝试运行应用程序时,出现以下异常:

mappingBy引用了一个未知目标实体属性:IncludedSite.ctas中的CtaContractBase.includedSite

另一个解决方案似乎是用@MappedSuperClass替换SCContract中的@Entity批注,但这会导致另一个异常(使用针对未映射类的@OneToMany或@ManyToMany:StudyContract.contracts [SCContract]),因为在另一个类(StudyContract)中,

@OneToMany(fetch = FetchType.LAZY, mappedBy = "studyContract", targetEntity = SCContract.class)
@BatchSize(size = 10)
private Set<SCContract> contracts;

而且正如博客所解释的那样,使用这种方法不再可能具有超类的集合。

还有其他解决方法还是我缺少什么?

IncludedSite的关联定义为

@OneToMany(fetch=FetchType.LAZY, mappedBy = "includedSite")
private Set<CtaContractBase> ctas = new HashSet<CtaContractBase>();

所以Hibernate的查找类型的属性IncludedSite命名includedSite在类CtaContractBase 没有这样的领域。 该字段仅存在于子类SCContract 这意味着只能将SCContract实例作为此关联的目标,因此应将关联定义为

@OneToMany(fetch=FetchType.LAZY, mappedBy = "includedSite")
private Set<SCContract> ctas = new HashSet<SCContract>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM