Skip to content

NullPointerException when using Specification.where(null) with CriteriaBuilder #3738

@loic-brtd

Description

@loic-brtd

Hi, I wanted to to use Specification.where(null) as a "match all" org.springframework.data.jpa.domain.Specification (a specification without any restriction).

But I get a NullPointerException inside Hibernate code when using it with a CriteriaBuilder:

Specification<Task> matchAllSpecification = Specification.where(null);

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Task> query = cb.createQuery(Task.class);

Root<Task> root = query.from(Task.class);
query.where(matchAllSpecification.toPredicate(root, query, cb));

entityManager.createQuery(query).getResultList();

Stack trace :

java.lang.NullPointerException: Cannot invoke "org.hibernate.query.sqm.tree.expression.SqmExpression.getExpressible()" because "booleanExpression" is null

	at org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate.<init>(SqmBooleanExpressionPredicate.java:38)
	at org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate.<init>(SqmBooleanExpressionPredicate.java:31)
	at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.wrap(SqmCriteriaNodeBuilder.java:497)
	at org.hibernate.query.sqm.tree.select.SqmQuerySpec.setRestriction(SqmQuerySpec.java:362)
	at org.hibernate.query.sqm.tree.select.AbstractSqmSelectQuery.where(AbstractSqmSelectQuery.java:315)
	at org.hibernate.query.sqm.tree.select.SqmSelectStatement.where(SqmSelectStatement.java:373)
	at org.hibernate.query.sqm.tree.select.SqmSelectStatement.where(SqmSelectStatement.java:46)
	at org.hibernate.bugs.MatchAllSpecificationBugTest.findWithCriteriaBuilder(MatchAllSpecificationBugTest.java:86)
	at org.hibernate.bugs.MatchAllSpecificationBugTest.specificationWhereNull_criteriaBuilder(MatchAllSpecificationBugTest.java:56)

Argument of Specification#where(Specification<T>) is marked as @Nullable so I thought this should have worked.

Also, using this specification with a JpaRepository / JpaSpecificationExecutor (taskRepository.findAll(Specification.where(null))) seems to work fine as a "match all" query.

Also, I found a workaround:

Specification<Task> matchAllSpecification = (root, query, cb) -> cb.conjunction();

I have created a minimal Maven project with tests to reproduce the bug : https://github.com/loic-brtd/hibernate-bug-specification-to-predicate.

I'm using :

  • org.springframework.data:spring-data-jpa:3.4.1
  • org.hibernate.orm:hibernate-platform:6.6.4.Final

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions