我是Hibernate的新手。我在OneToMany
和User
之间建立了Expense
映射。我正在尝试为User
返回last week
的费用。
这是我正在使用的MySQL查询。
select SUM(amount) from Expense INNER JOIN User ON Expense.user_id = User.id AND User.username ='testUser' WHERE created >= curdate() - INTERVAL DAYOFWEEK(curdate())+1 DAY AND created < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY;
当我尝试在hibernate中使用此查询时,我得到HibernateQueryException
String query = "select SUM(amount) from Expense INNER JOIN User ON Expense.user_id = User.id AND user.username ='sarvam' WHERE created >= curdate() - INTERVAL DAYOFWEEK(curdate())+1 DAY AND created < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY";
List list = session.createQuery(query).list();
我得到的错误是 -
Exception in thread "main" org.hibernate.QueryException: outer or full join must be followed by path expression [select SUM(amount) from com.challenge.pojo.Expense INNER JOIN User ON Expense.user_id = User.id AND user.username ='sarvam' WHERE created >= curdate() - INTERVAL DAYOFWEEK(curdate())+1 DAY AND created < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:233)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:193)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1825)
at com.challenge.dao.ExpenseDAO.getExpensesForLastWeek(ExpenseDAO.java:52)
at com.challenge.dao.ExpenseDAO.getExpensesForLastWeek(ExpenseDAO.java:44)
at com.challenge.dao.Test.main(Test.java:27)
Caused by: org.hibernate.QueryException: outer or full join must be followed by path expression
at org.hibernate.hql.internal.classic.FromParser.token(FromParser.java:253)
at org.hibernate.hql.internal.classic.ClauseParser.token(ClauseParser.java:93)
at org.hibernate.hql.internal.classic.PreprocessingParser.token(PreprocessingParser.java:118)
at org.hibernate.hql.internal.classic.ParserHelper.parse(ParserHelper.java:43)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:223)
... 10 more
任何人都可以帮我修复它。
答案 0 :(得分:1)
createQuery(String queryString)为。创建一个新的Query实例 给出HQL查询字符串。
createSQLQuery(String queryString) 为给定的SQL查询字符串创建一个新的SQLQuery实例。
您正在使用第一个期望HQL作为输入的人,对于使用本机SQL,您可以使用第二个。
update t
set col = replace(col, ' ', ', ')
where col like '% %';
答案 1 :(得分:0)
您可能需要查看:I can't make a inner join between two tables in hibernate hql query
如果使用HQL,您必须使用面向对象的样式,它们可能如下所示:
String query = "select SUM(amount) from Expense exp INNER JOIN User u ON e.user_id = u.id AND u.username = 'sarvam' ...";
List list = session.createQuery(query).list();
(未测试的)
或者,如果您改为使用:
,则可以继续使用标准SQL合成代码List list = session.createSQLQuery(query).list();
然后您可能想要阅读Hibernate文档的Chapter 16。
请记住,SQL方言取决于底层数据库。根据您的应用程序,可能在不同的系统上使用不同的数据库,这可能会破坏您的SQL语句。