2017-10-15 123 views
0

许多-to-many关联我有一个许多一对多协会在同一User实体,以允许用户跟随其他用户。一切都按预期工作。休眠数NamedQuery与同一实体

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

我试图让追随者的数量的计数分页。我显然可以使用User.getFollowers.size()来计数,但这样相当昂贵。我决定使用命名查询来代替它,因为它似乎是合理的做法。

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

我知道,因为我得到一个错误,但无法弄清楚如何解决它,这是不对的。这是堆栈跟踪:

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) 

任何帮助表示赞赏。

+0

错误堆栈真的会帮助我们。 –

回答

1

这是用户和追随者之间的多对多关系,因此应该在查询中加入JOIN。

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

这会查询特定用户ID的关注者数量。

+0

我快到了。我有SELECT COUNT(u)FROM用户u INNER JOIN u.followers fl WHERE fl.user =:userId。显然没有工作,但我很高兴我接近解决方案,因为SQL不是我的东西。非常感谢答案的人。 –