这是在JSF + JPA Web应用程序上。
当我使用包含强制转换的以下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;
}
我有一个Stock类,用于存储有关股票的数据。它有一个表示批处理的ItemBatch类。批处理有一个项目。那个项目实际上是一个Amp,它扩展了Item。 Amp有一个Vmp。我想通过Vmp获得股票。如果ItemBatch具有Amp的属性,那将非常容易,但我不得不使用更通用的Item作为参考。所以我需要在Java Resistance Query中将Item转换为Amp。我尝试了但是它出现了以下错误。
我使用EclipseLink 2.5作为持久性提供者。
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 :(得分:0)
我没有解决这个问题,但做了一个解决方案。我将子类中的所有其他方法添加到父类中,以便不需要进行转换。