0

I'd like to ask how to create hibernate criteria to get these employees who has more than one project. I have following entities

class Project{
    public String title;
    public Employee owner;    
}

class Employee{
    public String name;
    public Set<Project> projects;
}

I think I need to use query with subquery and the subquery should probably look like this:

DetachedCriteria sub = DetachedCriteria.forClass(Project.class)
  .createAlias("employee","e")
  .setProjection(Projections.projectionList()
    .add(Projections.count("owner"))
    .add(Projections.groupProperty("e.name"))
  );

(it returns list of objects with owner name and number of his projects), but I don't know how to use it with main criteria query, to compare if projects number is greater than 1.

This hql returns the same results I want to get with criteria:

from Project project inner join project.owner owner group by owner.name having count(project.owner) > 1
akn
  • 3,712
  • 26
  • 43

0 Answers0