您好,我正在尝试从数据库部门实施查询(包含案例陈述),但我无法弄清楚问题出在哪里。
@Query("select new com.withgratitude.api.core.domain.ReportTopTenBiddersTable( CONCAT(u.firstName, ' ', u.lastName), " +
"b.auctionId, b.date), " +
" case when (b.isTheWinner = 0) then 'No' when (b.isTheWinner = 1) then 'Yes' else 'No Winner' end as winnerBid" +
" from Bids b, Users u " +
" where b.userId = u.userId " +
" order by CONCAT(u.firstName, ' ', u.lastName)")
List<ReportTopTenBiddersTable> reportTopTenBiddersTable();
Here is the Query I am trying to implement:
#top 10 bidders - table
select CONCAT(u.first_name, ' ', u.last_name) as name, b.auction_id, b.date,
case b.is_thewinner when 0 then 'No' when 1 then 'Yes' end as WinnerBid, b.offer
from bids b, users u
where b.user_id = u.user_id
order by name
limit 10;
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, column 130 [select new com.withgratitude.api.core.domain.ReportTopTenBiddersTable( CONCAT(u.firstName, ' ', u.lastName), b.auctionId, b.date), case when b.isTheWinner = 0 then 'No' when b.isTheWinner = 1 then 'Yes' else 'No Winner' end as winnerBid from com.withgratitude.api.core.dao.Bids b, com.withgratitude.api.core.dao.Users u where b.userId = u.userId order by CONCAT(u.firstName, ' ', u.lastName)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:296) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:188) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
... 83 common frames omitted
答案 0 :(得分:0)
要添加到Andronicus答案中,我想您可以使用nativeQuery
并以纯SQL编写数据库请求。
@Query(value = "select CONCAT(u.first_name, ' ', u.last_name) as name, b.auction_id, b.date, " +
"case b.is_thewinner when 0 then 'No' when 1 then 'Yes' end as WinnerBid, b.offer " +
"from bids b, users u " +
"where b.user_id = u.user_id " +
"order by name " +
"limit 10", nativeQuery = true)
List<ReportTopTenBiddersTable> reportTopTenBiddersTable();
答案 1 :(得分:0)
这里有很多问题-其中一个是返回类型(ReportTopTenBiddersTable对象的列表),它似乎没有地方保留完整的出价信息。
这些部分不合适(有sql和jpql混合使用),类型不对齐-但是很难从这个简短的示例中看出错误的根源-以及最佳解决方案。
最简单的解决方案之一是将出价信息添加到ReportTopTenBiddersTable对象;这样,您可以删除case
(但仍保留数据)并保持签名不变:
更改构造函数ReportTopTenBiddersTable
以接受“出价”对象(并将其存储在字段中)。
在查询开始时将构造函数调用更改为包含b
(您可以省略日期,因为无论如何它都可以从b
访问):
select new com.withgratitude.api.core.domain.ReportTopTenBiddersTable(
CONCAT(u.firstName, ' ', u.lastName), b.auctionId, b.date) ...