使用JOIN和COUNT

时间:2018-02-21 12:04:43

标签: java hibernate detachedcriteria

我有两个实体:

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

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

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

和第二:

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

任何人都可以告诉我如何获得hibernate标准以获得每个团队中拥有多个用户的团队,如何将此查询转换为DetachedCriteria:

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 个答案:

没有答案