Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions