参数类型与sum函数不匹配

时间:2013-08-26 20:31:02

标签: jpa-2.0 eclipselink criteria-api

我得到了“参数类型不匹配”,其中sum-function映射到非托管的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 个答案:

答案 0 :(得分:2)

我希望它返回Long,因为这与JPQL一致。在JPA 2.0规范中,SUM函数描述如下:

  

当应用于整数类型的状态字段时,SUM返回Long(其他   比BigInteger);应用于浮动状态字段时加倍   点类型;应用于类型的状态字段时的BigInteger   BigInteger的;和BigDecimal应用于类型的状态字段时   BigDecimal的。

Hibernate似乎也是这样工作的,返回类型是Long

如果首选Long,则返回类型也会受到EclipseLink的影响。这可以通过CriteriaBuilder.sumAsLong

完成
final Expression<Long> sumActiveExp = cb.sumAsLong(activeExp);