2013-08-26 34 views
1

我得到「參數類型不匹配」與總和函數映射到非託管jpa實體。參數類型與總和函數不匹配

查詢:

final Path<String> departmentPath = root.get(SomeEntity_.department); 
final Path<Status> statusPath = root.get(SomeEntity_.status); 

final Predicate statusActivePred = cb.equal(statusPath, Status.ACTIVE); 
final Expression<Integer> activeExp = cb.<Integer> selectCase().when(statusActivePred, Integer.valueOf(1)).otherwise(Integer.valueOf(0)); 
final Expression<Integer> sumActiveExp = cb.sum(activeExp); 

query.select(cb.construct(SomeInfo.class, departmentPath, sumActiveExp)); 
... 

映射級:

public class SomeInfo 
{ 
    private final String department; 

    private final Integer someCount; 

    public SomeInfo(final String department) 
    { 
    super(); 
    this.department= department; 
    } 

    public SomeInfo(final String department, final Integer someCount) 
    { 
    super(); 
    this.department= department; 
    this.someCount = someCount; 
    } 
... 

例外

java.lang.IllegalArgumentException: argument type mismatch 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
at org.eclipse.persistence.internal.security.PrivilegedAccessHelper.invokeConstructor(PrivilegedAccessHelper.java:389) 
at org.eclipse.persistence.queries.ReportQueryResult.buildResult(ReportQueryResult.java:121) 
at org.eclipse.persistence.queries.ReportQueryResult.<init>(ReportQueryResult.java:78) 
at org.eclipse.persistence.queries.ReportQuery.buildObject(ReportQuery.java:593) 
at org.eclipse.persistence.queries.ReportQuery.buildObjects(ReportQuery.java:644) 
at org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:847) 
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852) 

事情是這樣的波紋管工作,但不與求和函數的表達式

query.select(cb.construct(SomeInfo.class, departmentPath)); 

任何提示?

+1

嘗試使用Object作爲SomeInfo.someCount的類型,並檢查對象的類型。我的猜測是總和返回爲Long,而不是整數。 –

+0

感謝您的提示。由於某種原因,我發現它是一個BigDecimal。現在最大的問題是:我的代碼或EclipseLink是否錯誤? – user871611

回答

2

我預計它會返回Long,因爲這與JPQL一致。在JPA 2.0規範SUM函數被描述爲如下:當施加到積分類型(其他 除BigInteger)的狀態字段

SUM龍返回;雙精度浮點型應用於浮點狀態字段 ; BigInteger應用於 類型的狀態字段BigInteger;和BigDecimal應用於類型爲 BigDecimal的狀態字段。

另外Hibernate似乎這樣工作,返回類型是Long

如果首選Long,則返回類型也會受到EclipseLink的影響。這可以通過CriteriaBuilder.sumAsLong完成:

final Expression<Long> sumActiveExp = cb.sumAsLong(activeExp); 
+0

cb.sumAsLong是我最終使用的。 – user871611