Create hibernate DetachedCriteria with JOIN and COUNT

114 Views Asked by At

I have two entities:

@Entity
@Table(name="team")
public class Team { 

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column(unique = true)
  private String name;
}

and second:

@Entity
@Table(name="user")
public class User { 

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column
  private String name;

  @Column
  private String surname;

  @ManyToOne(cascade={},fetch=FetchType.LAZY)
  @JoinColumn(name="team_id")
  private TeamEntity team;

  @Column(name="team_id", insertable=false, updatable=false)
  private Long teamId;
}

Can anyone tell me how can I get hibernate criteria to get teams with number of users in every team, how convert to DetachedCriteria this query:

select t.*, COUNT(u.id) as usercount from team t LEFT JOIN user u ON t.id = u.team_id GROUP BY t.id ORDER BY usercount, t.name;

?

0

There are 0 best solutions below