많은-to-many 연관에 대한 NamedQuery을 계산합니다. 모든 것이 예상대로 작동합니다. 최대 절전 모드 나 사용자가 다른 사용자를 수행 할 수 있도록 동일한 <code>User</code> 개체에 <strong><em>대다</em></strong> 협회가 같은 개체
@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)
어떤 도움을 주실 수 있습니다.
오류 스택이 실제로 우리를 도울 것입니다. –