2014-07-10 41 views
0
中投射時出錯

這是在JSF + JPA Web應用程序上。在JPQL

當我使用以下包含投射的JPQL查詢時,它會給出一個難以理解的錯誤。

這是查詢

jpql = "select new data.dataStructure.PharmacyStockRow(amp.vmp.name, sum(s.stock), " 
+ "sum(s.itemBatch.purcahseRate * s.stock), sum(s.itemBatch.retailsaleRate * s.stock))" 
+ "from Stock s join treat(s.itemBatch.item as Amp)amp " 
+ "where s.stock>:z and s.department=:d " 
+ "group by amp.vmp "; 
m.put("d", department); 
m.put("z", 0.0); 

這是創建只是接收來自查詢結果的類。

package data.dataStructure; 

public class PharmacyStockRow { 
    String code; 
    String name; 
    Double qty; 
    Double purchaseValue; 
    Double saleValue; 

    public PharmacyStockRow() { 
    } 

    public PharmacyStockRow(String name, Double qty, Double purchaseValue, Double saleValue) { 
     this.name = name; 
     this.qty = qty; 
     this.purchaseValue = purchaseValue; 
     this.saleValue = saleValue; 
    } 

我有一個存儲有關股票的數據的股票類。它有一個代表批次的ItemBatch類。批次有一個項目。該項目實際上是一個Amp,它擴展了Item。 Amp有一個Vmp。我想通過Vmp獲得股票。如果ItemBatch的屬性爲Amp,那將會非常容易,但我必須使用更多的通用Item作爲參考。所以我需要在Java Resistance Query中將Item投射到Amp。我嘗試過,但它給出了以下錯誤。

我使用EclipseLink 2.5作爲Persistance Provider。

This is the class diagram.

Caused by: Exception [EclipseLink-6034] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException 
Exception Description: Invalid query item expression [ 
Query Key vmp 
    Query Key item (entity.pharmacy.Amp) 
     Query Key itemBatch 
     Base entity.pharmacy.Stock]. 
Query: ReportQuery(referenceClass=Stock jpql="select new data.dataStructure.PharmacyStockRow(amp.vmp.name, sum(s.stock), sum(s.itemBatch.purcahseRate * s.stock), sum(s.itemBatch.retailsaleRate * s.stock)) from Stock s join treat(s.itemBatch.item as Amp)amp where s.stock>:z and s.department=:d group by amp ") 
    at org.eclipse.persistence.exceptions.QueryException.invalidExpressionForQueryItem(QueryException.java:622) 
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.getLeafDescriptorFor(ObjectLevelReadQuery.java:1460) 
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.getLeafMappingFor(ObjectLevelReadQuery.java:1408) 
    at org.eclipse.persistence.internal.queries.ReportItem.initialize(ReportItem.java:124) 
    at org.eclipse.persistence.queries.ConstructorReportItem.initialize(ConstructorReportItem.java:137) 
    at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1044) 
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613) 
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:823) 
    at org.eclipse.persistence.queries.DatabaseQuery.prepareCall(DatabaseQuery.java:1741) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:268) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:190) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:142) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:126) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1475) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1497) 
+0

我不確定'sum(...)'內部的乘法是否有效JPQL – DannyMo

+0

我以前做過。我已經單獨更改了所有內容,並發現當鑄造變量處於group by子句時出現錯誤。 –

回答

0

我沒能解決的問題,但周圍做了工作。我將子類中的所有其他方法添加到父類中,以便不需要投射。