Spring数据jpa投影无法提取ResultSet [英] Spring data jpa projection could not extract ResultSet

查看:30
本文介绍了Spring数据jpa投影无法提取ResultSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下查询将我的实体映射到投影,但出现错误

I'm trying to map my entity to projection using the below query but i'm getting error as

异常:无法提取 ResultSet SQL [n/a];嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet

Exception : could not extract ResultSet SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

这里是查询

@Query("select rf.rfqID as rfqID,rf.creationDate as creationDate," +
   "rf.deadLineDate as deadLineDate,rf.details as details," +
   "rf.message as message, rf.rfqDoc as rfqDoc," +
   "CASE WHEN (rf.creationDate > CURRENT_DATE) THEN 'open' ELSE 'closed' END as status," +
   "rf.rfqMembers as rfqMembers " +
   "from RFQ rf where rf.createdBy = ?1")
Page<RfqDto> loadAllRfq(String creator, Pageable pageable);

在我的 Dto 中,我有一个额外的状态列,我不想将其保留在 db 中,并希望通过查询获取状态

In my Dto I have an extra status column which I don't want to persist in db and would like to get the status via query

这是我的投影界面

public interface RfqDto {
    String rfqID();
    Date creationDate();
    Date deadLineDate();
    String details();
    String message();
    String rfqDoc();
    String status();
    List<RfqMember> rfqMembers();
}

推荐答案

问题的根源在这里:

在我的 Dto 中,我有一个额外的状态列,我不想保留在 db 中并希望通过查询获取状态

In my Dto I have an extra status column which I don't want to persist in db and would like to get the status via query

正如文档中所述:

这里的重要一点是这里定义的属性与聚合根中的属性完全匹配....查询执行引擎在运行时为每个返回的元素创建该接口的代理实例,并将对公开方法的调用转发给目标对象.

The important bit here is that the properties defined here exactly match properties in the aggregate root. ... The query execution engine creates proxy instances of that interface at runtime for each element returned and forwards calls to the exposed methods to the target object.

因此,您不能为您的案例使用 spring 数据 jpa 投影.你不能使用 hibernate/jpa 投影 也是,因为它不支持行结果中的集合.

So, you can not use spring data jpa projection for your case. You can not use hibernate/jpa projection as well, because it dose not support collections in row results.

您可以尝试使用Blaze-Persistence Entity Views.参见例如这个答案.

You can try to use Blaze-Persistence Entity Views. See for example this answer.

这篇关于Spring数据jpa投影无法提取ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆