我有两个m:n关系的表。我想通过SQL(HQL)语句计算组中的所有用户(以获得具有最多用户的组)
public class User{
private String name;
@ManyToMany
private Set<Usergroup> groups;
}
public class Usergroup{
private String name;
@ManyToMany
private Set<User> users;
}
我写了一个HQL,但我总是遇到异常:你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在')
附近使用正确的语法这是我的HQL:
@NamedQuery(name = "Group.countUsers", query = "SELECT g, count(g.users) FROM Group g JOIN FETCH g.users u")
我做错了什么?
答案 0 :(得分:0)
试试吧
SELECT g, count(g.users) FROM Group g JOIN FETCH g.users as u
答案 1 :(得分:0)
我找到了解决方案......它是
@NamedQuery(name = "Group.countUsers", query = "SELECT g, count(u) FROM Group g JOIN FETCH g.users u GROUP BY g")