Hibernate JOIN错误语法异常

时间:2014-11-26 04:05:47

标签: mysql sql hibernate select join

在我的项目(java struts 2,hibernate)中,我想选择2表之间的连接。

Class User {
  private int userId;
  private String userName;
  private String dateOfBirth;
}

Class Bill {
  private int billId;
  private String dateOfBill;
  private Double moneyOfBill;
  private User user;
}

当我直接尝试使用localhost DB中的sql时,这是可以的

Select * From user u JOIN bill b ON (b.userId=u.userId) Group by b.userId Order by Sum(b.moneyOfBill) asc;

但是我的程序通过hibernate hql

是错误的
From User U JOIN Bill B ON (B.user.userId=U.userId) Group by B.user.userId Order by Sum(B.moneyOfBill) ASC;

eclipse控制台中的错误:

  

at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)       在org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)       在org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)       在org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:281)       在org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)       在org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)       在org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:101)       在org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:80)       在org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)       在org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)       在org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)       在org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT U.userId, U.userName, U.dateOfBirth
FROM Bill B 
JOIN USER U 
GROUP BY U.userId, U.userName, U.dateOfBirth 
ORDER BY SUM(B.moneyOfBill) ASC;