question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to shorten/alias method name derived queries. [DATACMNS-1213]

See original GitHub issue

justin mccune opened DATACMNS-1213 and commented

Many think that it’s great to use the name of the method to infer the SQL for a query on a repository. However, often the use of descriptive field names and one or two concerns (an AND operation with a SORT ordering) leads to incredibly long (or “ugly” names). For example one properly named long field and a simple boolean & sort order lead to the repository method name being 96 characters long. This destroys many style guides line-lengths when used.

While it is an alternative to use @Query make a shorter method name and write the actual query by hand, or to use java’s “default” on interface to alias a shorter form that uses the full / ugly query. They both are not as elegant as the simplicity of the named query where preferably I alias it.

Often the alias / shorter name is just as meaningful and clear in meaning even if omitting implicit/assumed context-- see examples below.

This has come up several times with coworkers (in multiple companies) & a quick search has shown that others have the same concern.

https://stackoverflow.com/questions/30019408/how-to-shorten-names-of-query-methods-in-spring-data-jpa-repositories

When I read the above, I thought perhaps I’d found the solution-- but it was a suggestion that I couldn’t find in the Spring Data feature request list, so I thought I’d add it.

So what I’d like would be something like the below-- where the annotation name is used instead of the actual method name to build the query.

@QueryByMethodName("findOneByDeletedIsFalseAndEmail")
User findOneByEmail(String email);

@QueryByMethodName("findOneByReallyLongFieldNameAndActiveStatusIsTrue")
Entity findOneByRLFN(ReallyLongFieldNameType value)

This improves both simplicity and readability. And I’m not particular about the annotation name.


16 votes, 8 watchers

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:50
  • Comments:6

github_iconTop GitHub Comments

6reactions
gbraleighcommented, Oct 18, 2021

Yes, please! This seems like a relatively simple feature that would be very valuable, improving our ability to read and easily understand repository generated method names.

1reaction
grzechupcommented, Dec 6, 2022

if you extend your repository with JpaRepository then you can use a default method which calls your origin repository method: public interface UserRepository extens JpaRepositry<String, User> { List<User> findUserByCreatedDateAndHairColorAndFavoriteTeamAndBLaBla(param1, param2...);

default findBlaBlaUser(param1, param2...) {

return findUserByCreatedDateAndHairColorAndFavoriteTeamAndBLaBla(param1, param2...); } }

in your service you can now call myUserRepo.findBlaBlaUser(param1, param2…)

Yeah you can use this trick but still, it isn’t pleasant when you enter your repository and see your long basic methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Derived Query Methods in Spring Data JPA Repositories
Learn different ways to sort results in Spring Data queries. ... Derived method names have two main parts separated by the first By...
Read more >
How to shorten names of query methods in Spring Data JPA ...
Notice each method has " DeletedIsFalse " in it. Is there a simple way to make method names shorter? Like i.e.: @FullMethodName("findOneByDeletedIsFalseAndEmail ...
Read more >
Derived Queries with Spring Data JPA – The Ultimate Guide
Spring Data JPA generates simple queries based on the name of a method in your repository. Your method names just have to follow...
Read more >
Spring Data JPA - Reference Documentation
The repository proxy has two ways to derive a store-specific query from the method name: By deriving the query from the method name...
Read more >
Derived Query Methods in Spring Data JPA
For simple use cases, you can write a derive query method by looking at the corresponding method name in the entity class. Just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found