一对多关系返回相同的对象

时间:2019-03-07 03:43:48

标签: hibernate jpa spring-data

我使用Spring Boot 2。

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {

    @OneToMany(mappedBy = "sampling", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Samples> samples = new ArrayList<>();
    ...
}

@Entity
public class Samples {

    @EmbeddedId
    private SampleId id;

    @MapsId("samplingId")
    @ManyToOne(optional = false)
    private Samplings sampling;
    ...
}

我的查询很大,结果很奇怪。

@Query("SELECT distinct(s) FROM Samplings s "
            + "Join fetch s.samples sa "
            + "Join fetch sa.testSamples ts "
            + "Join fetch s.product p "
            + "Join fetch p.productType pt "
            + "Left Join fetch s.color c "
            + "Left Join fetch s.dimension d "
            + "Left Join fetch s.machine m "
            + "inner Join fetch m.factory.emails "
            + "Left Join Fetch ts.compressionTest as ct "
            + "Left Join Fetch ts.flexionTest as ft "
            + "Left Join Fetch ts.csaDurabilityTest as csaDt "
            + "Left Join Fetch ts.bnqDurabilityTest as bnqDt "
            + "Left Join Fetch ts.csaScallingTest as csaSt "
            + "Left Join Fetch ts.bnqScallingTest as bnqSt "
            + "Left Join Fetch ts.absorptionTest as at "
            + "WHERE "
            + "( ct.nonCompliant=true and ct.emailSent=false) "
            + "or (ft.nonCompliant=true and ft.emailSent=false) "
            + "or (csaDt.nonCompliant=true and csaDt.emailSent=false) "
            + "or (bnqDt.nonCompliant=true and bnqDt.emailSent=false) "
            + "or (csaSt.nonCompliant=true and csaSt.emailSent=false) "
            + "or (bnqSt.nonCompliant=true and bnqSt.emailSent=false) "
            + "or (at.nonCompliant=true and at.emailSent=false) "
            + "ORDER BY m.factory.name, m.name  asc")

我有3个样本的样本。其中之一不合规。

实际上,查询返回的样本有3个样本...但是相同样本的3倍

可能是休眠状态的错误

0 个答案:

没有答案