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 of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.