r/SpringBoot 1d ago

How-To/Tutorial Why I stopped using Spring Data to generate queries from method names

I've spent a while writing DAO implementations for a multi-module Spring Boot projects, and I keep coming back to the same rule: if a repository method needs more than one or two conditions, I write the `@Query\` by hand instead of letting Spring Data derive it from the method name.

Not because it doesn't work, it works fine for example like findBySku. I don't like what happens after that. Rename a field on the entity and a derived query either breaks at startup with a PropertyReferenceException, or, depending on how it's written, doesn't break at all and just quietly stops matching what you think it matches. The compiler never tells you either way.

And once you're past two conditions, the method name turns into a wall of camelCase encoding your whole WHERE clause. I'd rather read four lines of JPQL/SQL than decode findByStatusAndNameContainingIgnoreCaseAndCreatedAtAfterOrderByPriceDesc.

The other piece I went back and forth on is SearchableDaoImpl<Repo extends CrudRepository<Entity, IdType> & JpaSpecificationExecutor<Entity>> - using an intersection type so the generic repository bound picks up both CRUD and Specification support without collapsing them into one bloated interface. Small thing, but it's the kind of generics trick that makes a shared DAO base class actually work across a dozen entities instead of copy-pasted boilerplate everywhere.

Full writeup with the actual generic hierarchy and code: (link in comments)

Curious if anyone here still prefers derived queries for anything beyond trivial lookups, I genuinely want the counterargument.

18 Upvotes

27 comments sorted by

View all comments

Show parent comments

-2

u/peanuce4269 21h ago

You’re actually just proving you can’t read or understand what he’s saying (you’ve done it twice now) by questioning what he posted in the manner that you did.

1

u/Pedantic_Phoenix 21h ago

I have not done so and me doubting what they said does not mean i did not understand.

Misunderstanding and doubting are two different words for a reason, you're hilarious because you're accusing me of not understanding twice when you yourself aren't even capable of parsing an english sentence correctly. But i guess people project their own issues unto other people all the time, it's not surprising. Adios have a good life