java—使用条件的多对多模型获取重复行;为什么?

gmol1639  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(140)

我使用pivot表来创建与hibernate的多对多关系。我创建了3个模型称为 Requirement , Collaboration 以及 CollaborationRequirement 那个 CollaborationRequirement 是我的透视表。

public class Requirement {
    @Id
    private Long id;

    @OneToMany(mappedBy = "requirement", fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
    private Set<CollaborationRequirement> collaborationRequirement = new HashSet<CollaborationRequirement>();   
}

public class Collaboration {
    @Id
    private Long id;

    @OneToMany(mappedBy = "collaboration", fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
    private Set<CollaborationRequirement> collaborationRequirements = new HashSet<CollaborationRequirement>();
}

public class CollaborationRequirement {
    @Id
    private Long id;

    @ManyToOne(cascade = CascadeType.ALL)
    private Requirement requirement;

    @ManyToOne(cascade = CascadeType.ALL)
    private Collaboration collaboration;
}

现在,当我想从需求表中选择所有行时,我会得到重复的数据(正好是collaborationrequirement表中具有该需求id的行数)。
要求DAO:

public List<Requirement> findAll() {
    Session session = getSession();

    try {
        Criteria criteria;

        criteria = session.createCriteria(Requirement.class, "requirement");
        criteria.add(Restrictions.sqlRestriction("1=1"));

        criteria.addOrder(Order.desc("id"));
        List<Requirement> result =  criteria.list();

        return result;
    } finally {
        session.close();
    }
}

所以我的问题是为什么会出现这个问题?如何修复?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题