Don’t be afraid to take a shot!

Greg L. Turnquist
6 min readOct 7, 2023

A couple weeks ago, I pinged our fearless leader. I needed some direction before taking on a couple tasks. One of these tasks I was thinking about, which seemed tiny at the time, I joked “for THAT one, perhaps I should write a parser?”

He rolled his eyes. “We don’t need a parser.”

Two weeks later…

Spring Data JPA has a handy feature where you can dynamically feed it ORDER BY clauses with simple strings, e.g. Sort.by(“firstname”). This nicely gets turned into a JPA Criteria Expression object.

In the simplest of cases, this is an entity type’s property name. However, we have always offered this escape hatch where you can feed it ANYTHING. For example, you could feed it LENGTH(firstname) as your criteria to apply an ORDER BY to a query, where you order things not by the row’s firstname but instead by the length of the row’s firstname. Basically, anything that your JPA provider supports.

In my pre-op analysis, I uncovered a seemingly endless supply of hacks and workarounds on StackOverflow.com. I was beginning to think that this feature NEVER worked!

--

--

Greg L. Turnquist

Sr. Staff Technical Content Engineer at CockroachDB • YouTube Content Creator at https://youtube.com/@ProCoderIO • Best-Selling Author • Coffee Lover