无法使用请求结果类型为具有多个返回的查询创建类型查询 [英] Cannot create Typed Query for query with more than one return using request result type

查看:107
本文介绍了无法使用请求结果类型为具有多个返回的查询创建类型查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将oracle结果列表绑定到摘要列表.但是我的 summary 列表中有3个类定义为DB实体

I'm trying to bind oracle result list to a summary list. But my summary list has 3 classes defined as entities of DB

我有三个实体类别A,B,C

I have three entity classes A, B, C

Summary.class
{
    @Autowired
    private A a;

    @Autowired
    private B b;

    @Autowired
    private C c;

    //getters and setters
}

@Enity
class A{
Fields 1..n ;
}  // same goes for other classes definition

我通过以下查询获得结果,但结果无法转换为摘要对象

I get the results with following query, but the results cannot be cast to the Summary object

List<Summary> summaryList = entityManager.createQuery("from A a, B b, C c" +
                " where a.field1 = b.field1 and a.fValue = :fValue " +
                "and b.field3= c.field3", Summary.class)
                .setParameter("fValue ", fValue )
                .getResultList();

调试: 我确保结果列表不为空,并且如果不将其强制转换为对象,则在下面的查询中可以正常工作

Debugging: I made sure resultslist is not empty and below query works fine if I dont cast it to an object

List summaryList = entityManager.createQuery("from A a, B b, C c" +
                    " where a.field1 = b.field1 and a.fValue = :fValue " +
                    "and b.field3= c.field3")
                    .setParameter("fValue ", fValue )
                    .getResultList();

我看到的替代方法1是遍历summaryList并将其分配给这样的单个列表,我尚未对其进行测试,但是我认为它可能会产生类强制转换异常,因为之前强制执行dint工作

The alternative 1 I see is to iterate through summaryList and assign it them to individual lists like this, which I haven't tested yet but I think it might give a class cast exception since the casting dint work before

for (int i = 0; i < summaryList.size(); i++) {
   Summary s= (Summary) summaryList.get(i); // might be class cast Exception
   aList.add(s.getA());
   bList.add(s.getB());
}

我在想的替代方案2是 从db获得唯一的A类字段列表,将其转换为A的列表,执行3次蛮力直到我全部获得.

The alternative 2 I'm thinking is to get only class A fields list from db cast it to A's List, do it 3 times brute force till I get all of them.

下面是我在创建新问题之前先看过的一些问题

Below are some of questions I looked at before creating a new question

使用不同的类来组合多个实体类

获取列表回映射到pojo

请让我知道您的想法,我认为我的主要方法是行之有效的好方法.

Please let me know your thoughts, I'm thinking my main approach is good way to do it if it works.

推荐答案

您的JPQL选择语句"from A a, B b, C c"无法映射回Summary实体,JPA没有足够的信息来做到这一点.

Your JPQL select statement "from A a, B b, C c" can not be mapped back to Summary entity, JPA does not have enough info to do that.

如果按照您的逻辑,可以由A,B,C组成一个摘要实例,那么您可以使用类似
的构造函数

If in your logic, a summary instance can be composed from A, B, C then you can have a constructor like

public Summary(A a, B b, C c) {
       .............
}


并将您的选择陈述更改为


and changed your select statment to be

"select new Summary(a, b, c) FROM A a, B b, C c"

这篇关于无法使用请求结果类型为具有多个返回的查询创建类型查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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