1

我得到“参数类型不匹配”的 sum 函数映射到非托管 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)

像下面这样的东西有效,但不是 sum 函数的表达式

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

有什么提示吗?

4

1 回答 1

2

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

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

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

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

final Expression<Long> sumActiveExp = cb.sumAsLong(activeExp);
于 2013-08-27T21:30:13.347 回答