簡體   English   中英

無法使用請求結果類型為具有多個返回的查詢創建類型查詢

[英]Cannot create Typed Query for query with more than one return using request result type

我正在嘗試將oracle結果列表綁定到摘要列表。 但是我的摘要列表將3個類定義為DB實體

我有三個實體類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

我通過以下查詢獲得結果,但是結果無法轉換為Summary對象

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();

調試:我確保結果列表不為空,並且如果不將其強制轉換為對象,則下面的查詢工作正常

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並將其分配給這樣的單個列表,我尚未對其進行測試,但是我認為這可能會產生類強制轉換異常,因為之前的強制轉換工作

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次蠻力直到我將其全部獲取。

以下是在創建新問題之前我查看的一些問題

使用不同的類來組合多個實體類

獲取列表映射回pojo

請讓我知道您的想法,我認為我的主要方法是行之有效的好方法。

您的JPQL select語句"from A a, B b, C c"無法映射回Summary實體,JPA沒有足夠的信息來做到這一點。

如果按照您的邏輯,一個摘要實例可以由A,B,C組成,那么您可以擁有一個類似

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

並將您的選擇陳述更改為

  “從A a,B b,C c中選擇新的Summary(a,b,c)” 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM