Question

I'm getting "argument type mismatch" with the sum-function mapping to a non-managed jpa entity.

The query:

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));
...

The mapping-class:

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;
  }
...

The exception

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)

Something like this bellow works, but not the expression with the sum-function

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

Any hints?

Was it helpful?

Solution

I would expect it to return Long, because that is consistent with JPQL. In JPA 2.0 specification SUM function is described as follows:

SUM returns Long when applied to state fields of integral types (other than BigInteger); Double when applied to state fields of floating point types; BigInteger when applied to state fields of type BigInteger; and BigDecimal when applied to state fields of type BigDecimal.

Also Hibernate seems to works that way and return type is Long.

If Long is preferred, return type can be affected also with EclipseLink. That can be done via CriteriaBuilder.sumAsLong:

final Expression<Long> sumActiveExp = cb.sumAsLong(activeExp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top