spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.

Home Page:https://spring.io/projects/spring-data-jpa/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SimpleJpaRepository.delete(Specification<T> spec) throws NullPointerException [Kotlin]

ASaludo opened this issue · comments

Hello,

I have a problem using JpaSpecificationExecutor.delete using a Specification Object

I'm using Kotlin and the @nullable on top of the toPredicate function of Specification used line 477 with the null parameter of query seemed to be the problem

java.lang.NullPointerException: Parameter specified as non-null is null: ... at org.springframework.data.jpa.repository.support.SimpleJpaRepository.delete(SimpleJpaRepository.java:477)

Thanks for reporting. Care to give us a bit more context of what you're trying to archive?
I do see a potential nullability contract violation with Specification#toPredicate when no CriteriaQuery is present due to CriteriaBuilder#createCriteriaDelete(Class) not implementing it.

I'll give you all details :
Spring-jata-jpa : 3.2.5
Kotlin : 1.9.23

I'm trying to play this code :

    fun isToDelete(): Specification<Request> {
        return Specification<Entity> { root, _, builder ->
            LocalDateTime.now().minusDays(120)?.let { builder.lessThanOrEqualTo(root.get("date"), it) }
        }
    }
    fun delete() = entityRepository.delete(isToDelete())

But on the code of SimpleJpaRepository that implement the JpaSpecificationExecutor I hit the following line of code :

Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder);`

with that interface :

	@Nullable
	Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);

I suspect that Kotlin don't take @nullable when compiling those java class and a NPE happen because "null" is literally passed to a non null parameter.