我想使用此SQL查询来实现JPA查询:
String hql = "SELECT DATE(date) AS Date, SUM(volume) AS amount, COUNT(*) AS number_of_transactions " +
" FROM " + PaymentTransactionsDailyFacts.class.getName() + " WHERE (date BETWEEN :start_date AND :end_date )" +
" GROUP BY DATE(date)";
我尝试过:
String hql = "SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e.*) AS count " +
" FROM " + PaymentTransactionsDailyFacts.class.getName() + " e WHERE (date BETWEEN :start_date AND :end_date )" +
" GROUP BY DATE(date)";
但是我得到了错误:
expecting IDENT, found '*' near line 1, column 63 [SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e.*) AS count FROM .......PaymentTransactionsDailyFacts e WHERE (date BETWEEN :start_date AND :end_date ) GROUP BY DATE(date)]
将查询执行到JPA中的正确方法是什么?
答案 0 :(得分:1)
JPQL不知道*
是什么意思。相反,您只会说e
,而不是e.*
。
String hql = "SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e) AS count "
+ " FROM " + PaymentTransactionsDailyFacts.class.getName()
+ " e WHERE (date BETWEEN :start_date AND :end_date )"
+ " GROUP BY DATE(date)";
这是因为JPQL在 Java 实体上运行。请注意,您是从类名而不是表中说的。对象没有通配符,表明类中的任何字段。相反,您指定对象本身不为null表示存在一行。
SQL中的e.*
语法实质上是(尽管可能不是字面意思)说该行上至少存在一个非空列。因为它没有任何直接的方式来表示存在行。但是JPQL确实有办法说一个对象存在。那是基本的Java非空检查。因此,在JPQL中,您可以使用Java方式(尽管采用JPQL语法)而不是SQL方式。
另请参见