与两个外键的一对多关系

时间:2018-05-10 18:32:47

标签: java hibernate spring-mvc spring-data

我有两个实体形成多对多关系。每个Spectrum都与另一个Spectrum匹配,其相似度得分存储在SpectrumMatch中。

@Entity
public class Spectrum {
    private long id;
    private List<SpectrumMatch> matches;
    private List<SpectrumMatch> matches2;

    @OneToMany(targetEntity = SpectrumMatch.class,
        mappedBy = "querySpectrum",
        fetch = FetchType.LAZY,
        cascade = Cascade.ALL)
    public List<SpectrumMatch> getMatches()

    @OneToMany(targetEntity = SpectrumMatch.class,
        mappedBy = "matchSpectrum",
        fetch = FetchType.LAZY,
        cascade = Cascade.ALL)
    public List<SpectrumMatch> getMatches2()
}

@Entity
public class SpectrumMatch {
    private long id;
    @NotNull
    private Spectrum querySpectrum;
    @NotNull
    private Spectrum matchSpectrum;
    private double score;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "QuerySpectrumId", referencedColumnName = "Id")
    public Spectrum getQuerySpectrum();

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "MatchSpectrumId", referencedColumnName = "Id")
    public Spectrum getMatchSpectrum()
}

在此代码中,字段Spectrum.matches包含SpectrumMatch的所有实例,其中SpectrumMatch.querySpectrum.id等于Spectrum.id

但是,我希望字段Spectrum.matches包含SpectrumMatch的所有实例,其中 SpectrumMatch.querySpectrum.id {{1等于SpectrumMatch.matchSpectrum.id

是否有可能实现这种关系?

0 个答案:

没有答案