对三个表的复杂查询返回无效行

时间:2019-06-03 22:33:37

标签: java mysql hibernate

我只想获取具有相关支持配置的项目。我一直在与JUnit测试一起开发查询方法。最终查询将更复杂,涉及另一个表,但是此问题需要首先解决。 分配必须至少具有一个视频或一个测验,并且每个都有多个。我需要忽略那些都没有的东西。

我实现了一个简单的查询getListA(),并测试了一个测验getListB()或视频getListC()。他们按预期返回了3、1和1个项目。

查询:

public List<AssignEntity> getListA(Integer orgid)
{
    Query query = entityManager.createQuery(
        "SELECT asgn FROM AssignEntity asgn, SegmentEntity asege " + 
        "WHERE asgn.orgid = :org AND asgn.id = asege.assignid");
    query.setParameter("org", orgid);
    return query.getResultList();
}

public List<AssignEntity> getListB(Integer orgid)
{
    Query query = entityManager.createQuery(
        "SELECT asgn FROM AssignEntity asgn, SegmentEntity asege " + 
        "WHERE asgn.orgid = :org AND asgn.id = asege.assignid " + 
        "AND asege.quizid IS NOT NULL");
    query.setParameter("org", orgid);
    return query.getResultList();
}

public List<AssignEntity> getListC(Integer orgid)
{
    Query query = entityManager.createQuery(
        "SELECT asgn FROM AssignEntity asgn, SegmentEntity asege, ContentEntity cie " + 
        "WHERE asgn.orgid = :org AND asgn.id = asege.assignid " + 
        "AND asege.contentid = cie.id AND cie.typeid = :videoct");
    query.setParameter("org", orgid);
    return query.getResultList();
}

public List<AssignEntity> getListD(Integer orgid)
{
    Query query = entityManager.createQuery(
        "SELECT asgn FROM AssignEntity asgn, SegmentEntity asege, ContentEntity cie " + 
        "WHERE asgn.orgid = :org AND asgn.id = asege.assignid " + 
        "AND ((asege.quizid IS NOT NULL) OR (asege.contentid = cie.id AND cie.typeid = :videoct))");
    query.setParameter("org", orgid);
    return query.getResultList();
}

实体。吸气剂和二传手已被删除。

@Entity
@Table(name="assignments")
public class EntityAssignEntity implements Serializable
{
    @Id
    @GeneratedValue()
    private Integer id;

    private Integer orgid;
    private String name;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "assignmentid")
    private List<SegmentEntity> assignmentSegments;
}

@Entity
@Table(name="assignment_sequences")
public class EntityAssignmentSequencesModel implements Serializable
{
    @Id
    @GeneratedValue()
    private Integer Id;

    private Integer assignmentid;
    private Integer contentid;
    private Integer quizid;
    private Integer segmentnum;

    @ManyToOne
    @JoinColumn(name = "assignmentid", referencedColumnName = "id", insertable = false, updatable = false)
    private AssignEntity assignment;

    @ManyToOne
    @JoinColumn(name = "contentid", referencedColumnName = "id", insertable = false, updatable = false)
    private ContentEntity contentItem;
}

@Entity
@Table(name="content_items")
public class ContentEntity implements Serializable
{
    public enum ContentTypes
    {
        NONE, // should be 0 
        VIDEO, EXAM, OTHER;
    }

    @Id
    @GeneratedValue()
    private Integer id;

    private String name;

    @Enumerated(EnumType.ORDINAL)
    private ContentTypes typeid;
}

没有错误。复杂查询getListD()在应返回1时返回17个项目。我有另一个查询,与getListD()相同,只是它选择了cie。它还返回17个项目,其中许多甚至与orgid参数都不匹配。好像查询得到了错误的优先级,将其像WHERE (A AND B AND C AND D) OR (E AND F)一样对待。
有什么建议吗?

0 个答案:

没有答案