Hi everyone,
I’m migrating from hibernate 5.3.24 to hibernate 6.1.5.
The following code works fine with hibernate 5.3.24, but with the latest version I’m getting a NPE when my predicate list is empty.
Is this a bug?
On the Criteria API javadoc the where method should allow an empty list.
/**
* Modify the query to restrict the query result according
* to the conjunction of the specified restriction predicates.
* Replaces the previously added restriction(s), if any.
* If no restrictions are specified, any previously added
* restrictions are simply removed.
* This method only overrides the return type of the
* corresponding AbstractQuery
method.
* @param restrictions zero or more restriction predicates
* @return the modified query
*/
CriteriaQuery where(Predicate… restrictions);
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<CustomerDTO> cq = cb.createQuery(CustomerDTO.class);
Root<Customer> customerRoot = cq.from(Customer.class);
List<Predicate> predicateList = new ArrayList<>();
//SOME CONDITIONAL PREDICATES
Predicate[] predicates = predicateList.toArray(Predicate[]::new)
cq.select(cb.construct(CustomerDTO.class, customerRoot.get(Customer_.id), customerRoot.get(Customer_.name))).where(predicates);
TypedQuery<CustomerDTO> q = getEntityManager().createQuery(cq);
return q.getResultList();
Caused by: java.lang.NullPointerException
at org.hibernate//org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitWhereClause(BaseSemanticQueryWalker.java:413)
at org.hibernate//org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitQuerySpec(BaseSemanticQueryWalker.java:231)
at org.hibernate//org.hibernate.query.sqm.tree.select.SqmQuerySpec.accept(SqmQuerySpec.java:122)
at org.hibernate//org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitQueryPart(BaseSemanticQueryWalker.java:213)
at org.hibernate//org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitSelectStatement(BaseSemanticQueryWalker.java:135)
at org.hibernate//org.hibernate.query.sqm.tree.select.SqmSelectStatement.accept(SqmSelectStatement.java:213)
at org.hibernate//org.hibernate.query.sqm.tree.jpa.ParameterCollector.collectParameters(ParameterCollector.java:52)
at org.hibernate//org.hibernate.query.sqm.internal.SqmUtil.resolveParameters(SqmUtil.java:440)
at org.hibernate//org.hibernate.query.sqm.tree.select.SqmSelectStatement.resolveParameters(SqmSelectStatement.java:208)
at org.hibernate//org.hibernate.query.sqm.internal.DomainParameterXref.from(DomainParameterXref.java:79)
at org.hibernate//org.hibernate.query.sqm.internal.QuerySqmImpl.<init>(QuerySqmImpl.java:236)
at org.hibernate//org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:1302)
at org.hibernate//org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:187)
at org.jboss.as.jpa@27.0.0.Final//org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:119)