Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
loic-brtd opened this issue Jan 9, 2025 · 1 comment
Labels
status: invalid An issue that we don't feel is valid

Comments

@loic-brtd
Copy link

loic-brtd commented Jan 9, 2025

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 9, 2025
@mp911de mp911de added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 9, 2025
@mp911de
Copy link
Member

mp911de commented Jan 9, 2025

Specification.toPredicate(…) can return null, however, when your code directly evaluates toPredicate(…) then you should include a null check to avoid calling CriteriaQuery .where(null) as the JPA API doesn't accept null predicates (as indicated by the error message).

toPredicate(…) isn't intended to be called from application code, rather it's the SPI side for our executors.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants