简体   繁体   English

休眠计数NamedQuery用于与同一实体的多对多关联

[英]Hibernate count NamedQuery for many-to-many association with the same entity

I have a many-to-many association on the same User entity to allow users to follow other users. 我在同一User实体上具有多对多关联,以允许用户关注其他用户。 Everything works as expected. 一切正常。

@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(
    name = "user_follower", 
    joinColumns = {@JoinColumn(name="user")}, 
    inverseJoinColumns={@JoinColumn(name="follower")}
)
private List<User> followers;

@ManyToMany(mappedBy="followers", fetch = FetchType.LAZY)
private List<User> following;

I'm trying to get a count of the number of followers for pagination . 我正在尝试统计分页的追随者人数。 I can obviously get the count using User.getFollowers.size() but that's rather expensive. 我显然可以使用User.getFollowers.size()来获得计数,但这是相当昂贵的。 I decided to do it using a named query instead because it seems to be the reasonable thing to do. 我决定使用命名查询代替它,因为这似乎是合理的做法。

@NamedQuery(
    name = "User.countFollowers", 
    query = "SELECT COUNT(u) FROM User u WHERE u.followers=:userId"
)

I know this isn't right because I get an error but can't figure out how to fix it. 我知道这是不对的,因为我得到了一个错误,但不知道如何解决它。 This is the stacktrace: 这是堆栈跟踪:

Hibernate: /* User.countFollow */ select count(user0_.id) as col_0_0_ from user user0_ cross join user_follower followers1_, user user2_ where user0_.id=followers1_.user and followers1_.follower=user2_.id and .=?
DEBUG [2017-10-15 13:36:13,293] org.hibernate.SQL: /* User.countFollow */ select count(user0_.id) as col_0_0_ from user user0_ cross join user_follower followers1_, user user2_ where user0_.id=followers1_.user and followers1_.follower=user2_.id and .=?
WARN  [2017-10-15 13:36:13,345] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 1064, SQLState: 42000
ERROR [2017-10-15 13:36:13,346] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=1' at line 1
ERROR [2017-10-15 13:36:13,359] com.xxxx.xxxx.resources.Resource: There was a problem generating a response (SQLGrammarException). Please check the logs
! com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExceptionxxxx: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=1' at line 1
! at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
! at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
! at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
! at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
! at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
! at com.mysql.jdbc.Util.getInstance(Util.java:387)
! at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
! at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
! at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
! at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
! at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
! at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
! at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
! at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
! at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
! ... 77 common frames omitted
! Causing: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
! at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
! at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
! at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
! at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
! at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
! at org.hibernate.loader.Loader.getResultSet(Loader.java:2115)
! at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
! at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1874)
! at org.hibernate.loader.Loader.doQuery(Loader.java:919)
! at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
! at org.hibernate.loader.Loader.doList(Loader.java:2610)
! at org.hibernate.loader.Loader.doList(Loader.java:2593)
! at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
! at org.hibernate.loader.Loader.list(Loader.java:2417)
! at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
! at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
! at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
! at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
! at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
! at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:964)

Any help is appreciated. 任何帮助表示赞赏。

This is a many-to-many relationship between user and followers, therefore there should be a JOIN in your query. 这是用户和关注者之间的多对多关系,因此查询中应该有一个JOIN。

SELECT COUNT(u) FROM User u JOIN u.followers f WHERE u.userId =:userId

This queries the number of followers for a particular User ID. 这将查询特定用户ID的关注者数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM