Search code examples
hibernatemany-to-manyassociationshibernate-mappinghibernate-criteria

Criteria API: can I use Criteria with unidirectional ManyToMany association?


I have User entity:

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

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

And Project entity, which has unidirectional ManyToMany association with 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;

And then I would like to get all users assigned to specified project. Simplest way is to ask GroupDAO about project by ID and then get users in this project. But I think this is not his responsibility, UserDAO should take care about User. Is there any chance, with Criteria interface, to get combine Project entity with User to achieve this? I tried the following:

@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;
}

But it still "need" Project association in User entity. I would be happy if anybody decides to help me - thank you in advance for your time. Update: I probably found working but not elegant solution, but still I am asking you for help. Solution:

I also tried the following:

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;
}

but I got 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]. Then I gave up and decided to ask ProjectDAO about users in project:

@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;
}

And it seems to be ok, but still I would be happy if some of you would look at my problem to solve it in more elegant way - thanks in advance to all viewers.


Solution

  • I also tried the following:

    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;
    }
    

    but I got 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]. Then I gave up and decided to ask ProjectDAO about users in project:

    @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;
    }