0

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

我已经检查了这个博客上的解决方法,“单表,没有 mappedBy”看起来像一个解决方案,但我真的需要双向关联。

这些是我要映射的类:

拥有方

@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 {

...

}

尝试运行应用程序时出现此异常:

mappedBy 引用了一个未知的目标实体属性: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;

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

还有其他解决方法还是我错过了什么?

4

1 回答 1

1

中的关联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>();
于 2013-01-03T11:48:52.983 回答