Since Spring Boot 3.5.0 method where() from org.springframework.data.jpa.domain.Specification interface is deprecated and will be removed
/**
* Simple static factory method to add some syntactic sugar around a {@link Specification}.
*
* @param <T> the type of the {@link Root} the resulting {@literal Specification} operates on.
* @param spec can be {@literal null}.
* @return guaranteed to be not {@literal null}.
* @since 2.0
* @deprecated since 3.5.
*/
@Deprecated(since = "3.5.0", forRemoval = true)
static <T> Specification<T> where(@Nullable Specification<T> spec);
Description of this method doesn't explain how to replace it with another not deprecated method. Do we have another method to create empty specification without deprecation methods?
final var spec = Specification.where(null);
A casual glance at the docs says: Specification.not(null)
. However, that'd be a mistake: The docs are out of date with themselves. A more in-depth look at the source (at history of Specification.java) indicates that spring has apparently tossed the entire model out and built a completely new one. The method you want is completely gone, as is the ability to call not(null)
.
The replacement is unrestricted()
.
This does the exact same thing your old and now removed where
used to do.
Note that where the current docs smear 'spec
can be null
' all over it, this version aggressively NPEs when you attempt to use null
. This is correct:
null
should mean 'unknown', 'unset', or 'not relevant' - in other words, the NPE is a good thing, it indicates a programmer is asking questions about an object whose value is 'unknown' and there is no way to do that. "What is the length of this unknown string?" The answer is not '0'. There answer is: "I do not know, this code path needs to abort".
It should not mean 'not present'. This leads to annoyance, where the NullPointerException is annoying and feels 'wrong'.
In other words:
Update spring to the state of the art and rewrite all your specification code. Specification.where(null)
, specifically, turns into Specification.unrestricted()
, but you'll have quite some more rewrites to do. -or-
Don't worry about the deprecation just yet. There is no replacement available. Put in the agenda that you need to update this stuff before upgrading spring. So leave that deprecation intact as an in-the-code reminder that you cannot take your code base along with updates to spring jpa data.
NB: Do not rewrite it to not(null)
! The one and only thing that will do, is remove the deprecation warning. But that'd be in error: That code will fail to work exactly 'as fast' as where(null)
will fail to work - you're simply engaging in a hard to read, crazy way to write @SuppressWarnings("deprecation")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With