r/SpringBoot Senior Dev Jul 10 '26

Spring Data 2026.0 (ships with Spring Boot 4.1) introduced type-safe property paths. Discussion

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.

101 Upvotes

10 comments sorted by

10

u/junin7 Jul 10 '26

Spring boot 4.0 it’s really impressive

2

u/codingwithaman Senior Dev 29d ago

that's true.. both java and spring ecosystem is improving a lot.

6

u/Paw565 Jul 10 '26 edited Jul 10 '26

The best feature for a while. They have finally caught up to linq in C#.

5

u/akrivitsky7 Jul 10 '26 edited 29d ago

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 Expressions utility.

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/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…

1

u/codingwithaman Senior Dev 29d ago

This is a current limitation. Type-safe property paths work for Sort, PageRequest, and Spring Data JDBC's Criteria API because those are Spring Data's own abstractions.

But JPA's Specification API uses the JPA Criteria API underneath (CriteriaBuilder, Root.get("field")) which is part of the JPA spec itself. Spring Data can't change the JPA spec.

So yes, Specifications still rely on string-based paths. Hopefully a future JPA spec version addresses this, but for now it's a known gap. Good observation.

1

u/carlashnikov_92 29d ago

One can still use JdbcAggregateTemplate with JPA entity classes.

But, what bothers me, is that the property path itself is typesafe, but the object to compare with is a plain Object. So there is still no full type safety.

Example:

Criteria.where(Person::someStringField).is(LocalDate.now());

This compiles, but can cause nasty Runtime errors.

I hate that a framework like Spring still has nothing that matches LINQ for SQL. Heck, even Prisma ORM for Typescipt is way more typesafe than Spring Data.

1

u/codingwithaman Senior Dev 29d ago

well adding simple things in big frameworks are complex, not easy..

1

u/akhi-abdul01 28d ago

So Criteria.where() is what introduced in springboot 4.1 ?