NullPointerException using non-ANSI function in CriteriaQuery in Hibernate

Solution Verified - Updated -

Issue

  • Building a CriteriaQuery using SQL Server function datediff

                CriteriaBuilder cb = entityManager.getCriteriaBuilder();
                CriteriaQuery<Date> cq = cb.createQuery(Date.class);
                Root<Employee> root = cq.from(Employee.class);
                final Expression<BigInteger> diff = cb.function("datediff", BigInteger.class,
                        cb.literal("ms"),
                        root.get(Employee_.hireDate),
                        root.get(Employee_.someDate)
                ).as(BigInteger.class);
                cq.multiselect(diff);
                for (Object o : entityManager.createQuery(cq).getResultList()) {
                        log.info("ActiveTime: " + Date.class.cast(o).getTime());
                }
    
  • Encountering the error below

    java.lang.NullPointerException
        at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.determineJdbcTypeCode(StandardAnsiSqlAggregationFunctions.java:200)
        at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.getReturnType(StandardAnsiSqlAggregationFunctions.java:158)
        at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:414)
        at org.hibernate.hql.internal.ast.tree.AggregateNode.getDataType(AggregateNode.java:68)
        ...
        at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:130)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:699)
        ...
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content