使用JPQL中的条件计算Max

时间:2015-10-26 05:14:33

标签: hibernate jpa count jpql

您好我有OneToMany协会(用户---会员资格), 我需要找到具有更多成员资格的用户,其属性Membership.status等于'ACCEPTED'。

我在想:

@Query("select u from User u join u.memberships m where count(m) = "
        + "(select max(count(m1)) from User u1 join u1.memberships m1 where m1.status ="
        + " 'ACCEPTED') and m.status = 'ACCEPTED'")

控制台打印:

2015-10-26 16:45:13,514 [main] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - SQL Error: 1111, SQLState: HY000
2015-10-26 16:45:13,514 [main] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - SQL Error: 1111, SQLState: HY000
org.hibernate.exception.GenericJDBCException: could not extract ResultSet
2015-10-26 16:45:13,514 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - Invalid use of group function
2015-10-26 16:45:13,514 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - Invalid use of group function

你可以帮我解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

<强>更新:
尝试使用此查询:

SELECT u FROM user u
LEFT JOIN FETCH u.memberships  m
WHERE m.status = 'ACCEPTED'
AND u.memberships.size = (SELECT max(u1.memberships.size) FROM user u1)