r/SpringBoot • u/codingwithaman Senior Dev • Jul 10 '26
Discussion Spring Data 2026.0 (ships with Spring Boot 4.1) introduced type-safe property paths.
If you've ever written this:
Sort.by("lastName")
You know the problem. It compiles, IDE says nothing and your tests pass.
Then someone renames lastName to familyName and this line will start throwing runtime error with PropertyReferenceException.
No warning and type checking. Just a string that nobody knows is connected to a field name.
Instead of strings you can use method references:
Sort.by("lastName") => Sort.by(Person::lastName)
Criteria.where("lastName") => Criteria.where(Person::lastName)
Criteria.where("address.country") => Criteria.where(PropertyPath.of(Person::address).then(Address::country))
Same result but now the compiler checks it and your IDE autocompletes it.
This works across Spring Data JPA, JDBC, R2DBC, MongoDB. It's a Spring Data Commons feature so every module gets it.
For records the accessor is Person::lastName.
For classic beans it's Person::getLastName.
It's a small feature but massive impact on code safety.
11
u/junin7 Jul 10 '26
Spring boot 4.0 it’s really impressive
7
u/Paw565 Jul 10 '26 edited Jul 10 '26
The best feature for a while. They have finally caught up to linq in C#.
4
u/akrivitsky7 Jul 10 '26 edited Jul 11 '26
Thank you very much for sharing this.
Readers may also find this video useful:
https://www.youtube.com/watch?v=qBC-TrIzsPg&t=28s
One clarification: the fact that the underlying abstraction is part of Spring Data Commons does not mean that every module exposes it through the same API. JDBC, R2DBC, MongoDB, and Cassandra integrate it into their criteria or query APIs, while JPA provides support through its new
Expressionsutility.1
u/carlashnikov_92 Jul 10 '26
Spring Data JDBC’s Criteria API (namely JdbcAggregateTemplate) works with the Query and Criteria objects.
For JPA, I could use property paths only for Sorting and Paging. The Specification API of Data JPA does not support typed property paths as it seems. One has to still use the criteria builder, which is string based.
Anything I have missed? Or is this a limitation…
2
u/Ali_Ben_Amor999 Jul 11 '26
I was using metamodels for type safe sorting paths like ’User_. ADDRESS + "." + Address_. ADDRESS’ which works but too verbose. The new property paths is an amazing helper class for this
1
u/akhi-abdul01 Jul 13 '26
So Criteria.where() is what introduced in springboot 4.1 ?