-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A way to shorten/alias method name derived queries. [DATACMNS-1213] #1652
Comments
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. |
Method name gets too crowd just for a simple query, proposed approach seems convenient. |
any news on this proposal? |
I would love to see this feature. For example, I have the following method query in my repository. |
if you extend your repository with
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. |
Spring Data is not in charge of defining yet another query language. We've built query derivation to allow users conveniently declare simple queries without having to dive into the store-specific query language to implement common use cases. Putting the potential Furthermore, we are strictly against putting code into annotations that actually belong somewhere else. Such a change leads to removing information from the call site. The method name becomes less expressive at its best. In the worst case, it contradicts what was specified in an annotation. |
I speak here in the general case, and not specifically with respect to SD JPA. Perhaps a less-known feature of Spring Data, and unfortunately, it is not a feature that is supported across all Spring Data modules, but a nice alternative none-the-less is, some Spring Data modules support a
Building on my statement above with a concrete example, suppose you have a Repository with the following query method: interface CustomerRepository extends CrudRepository<Customer, Long> {
Optional<Customer> findVipCustomers(String firstName, String lastName);
} Then, the actual store-specific query could be defined in a Java Properties file (pseudo syntax used): // Properties defining store-specific queries used by all application declared Repositories
Customer.findVipCustomers=SELECT DISTINCT c.first_name, c.last_name, c.birth_date \
FROM Customers c \
WHERE c.first_name = ? \
AND c.last_name = ? \
AND c.vip = true \
ORDER BY c.last_name ASC, c.birth_date DESC Typically, the Java Properties file location/name is in To see how this works in Spring Data Repository infrastructure, there are few things you have to know. Internally, Spring Data (Commons) represents these externally sourced (e.g. Java Properties) and named queries with the The "name" of the query gets resolved from the The "location" of this Java Properties file defining the queries can be changed with Repository enabling annotation for the specific data store in use. For example, when using Spring Data Cassandra, you would use the Then, internally, and by example, Spring Data Cassandra's It is pretty eloquent. It seems to me, Spring Data JPA's With all the mucking with [derived] query method names (and overly used Although, I can empathize somewhat since our documentation does not explicitly describe this feature, and store-specific documentation in many cases is a just a blanket copy of Spring Data Commons' reference documentation, particularly for Spring Data Repository support. |
To summarize the different approaches for declaring queries with Spring Data's Repository infrastructure, the following common strategies can be used:
Other non-common approaches and forms of querying that some Spring Data modules may support include, but are not limited to: Query by Example or Querydsl, JOOQ, HQL, etc. Spring Data does not prevent or limit your choice; the limitation only occurs due to the underlying data store. |
Except query derivation is a query DSL. I can't see how to reconcile these two statements.
Instead of moving the problem to an annotation (
I don't understand rejecting I write this because I have observed in practice, that moving logic from query derivation to |
It might be a language thing, but “yet another” intended to mean “another one on top of the already existing (query derivation, store specific queries, Querydsl, query by example, custom implementations) ones”. Generally speaking, for each request, we have to judge different aspects:
Given that all these alternatives already exist, 1 is low by definition. 2 and 3 definitely increase, and we came to the conclusion that adding what's asked for is not worth the effort. Rest assured that we have a pretty good impression of how our users use repository query methods. |
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.
This improves both simplicity and readability. And I'm not particular about the annotation name.
16 votes, 8 watchers
The text was updated successfully, but these errors were encountered: