Criteria API:我可以将Criteria与单向ManyToMany关联一起使用吗?

时间:2015-03-10 20:57:38

标签: hibernate many-to-many associations hibernate-mapping hibernate-criteria

我有User实体:

@Entity
@Table(name = "users")
public class User implements Serializable{

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String name;

Project实体,它与ManyToMany具有单向User关联:

@Entity
@Table(name = "projects")
public class Project {
    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String name;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable
    private List<User> users;

然后我希望将所有用户分配到指定的项目。最简单的方法是通过ID询问GroupDAO项目,然后让用户参与此项目。但我认为这不是他的责任,UserDAO应该关注User。使用Criteria界面是否有机会将Project实体与User结合起来实现此目的?我尝试了以下方法:

@Override
public List<User> getByProjectId(Long projectId) {
    Criteria criteria = createCriteria(User.class);
    criteria.setFetchMode("project", FetchMode.JOIN);
    criteria.add(Restrictions.eq("project.id", projectId));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<User> users = (List<User>) criteria.list();
    return users;
}

Project实体中仍然“需要”User关联。如果有人决定帮助我,我会很高兴 - 提前感谢你的时间。 更新:我可能找到了工作但不是优雅的解决方案,但我仍在请求您的帮助。的解决方案:

我也尝试了以下内容:

public List<User> getByProjectId(Long projectId) {
    Query query = getCurrentSession().createQuery("from User as user right 
        join Project project where project.id=:projectId");
    query.setLong("projectId", projectId);
    List users = query.list();
    return users;
}

但我得到了org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]。然后我放弃了,并决定向ProjectDAO询问项目中的用户:

@Override
public List<User> getUsersInProject(Long projectId) {
    Criteria criteria = createCriteria(Project.class);
    criteria.createAlias("users", "u");
    criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    criteria.add(Restrictions.eq("id", projectId));

    List resultList = criteria.list();
    List<User> users = new ArrayList<User>();
    for (Object result: resultList){
        Map resultMap = (Map) result;
        users.add((User) resultMap.get("u"));
    }
    return users;
}

似乎还可以,但如果你们中的一些人会以更优雅的方式解决我的问题,我会很高兴 - 先谢谢所有观众。

1 个答案:

答案 0 :(得分:0)

我也尝试了以下内容:

public List<User> getByProjectId(Long projectId) {
    Query query = getCurrentSession().createQuery("from User as user right 
        join Project project where project.id=:projectId");
    query.setLong("projectId", projectId);
    List users = query.list();
    return users;
}

但我得到了org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]。然后我放弃了,并决定向ProjectDAO询问项目中的用户:

@Override
public List<User> getUsersInProject(Long projectId) {
    Criteria criteria = createCriteria(Project.class);
    criteria.createAlias("users", "u");
    criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    criteria.add(Restrictions.eq("id", projectId));

    List resultList = criteria.list();
    List<User> users = new ArrayList<User>();
    for (Object result: resultList){
        Map resultMap = (Map) result;
        users.add((User) resultMap.get("u"));
    }
    return users;
}