Dynamic query criteria
See original GitHub issueAt the moment query criteria is hard coded in the method name, so if we need in some cases to find products by created time starts with, and in another case to find products by created time ends with, we need to have 2 methods.
fun findByCreatedTimeStartsWith(createdTime: DateTime)
fun findByCreatedTimeEndsWith(createdTime: DateTime)
As solution can be dynamic query criteria
sealed class OrderedCriteria<T> {
class Gt<T>(value: T) : OrderedCriteria // greater than this value
class Gte<T>(value: T) : OrderedCriteria // greater than or equal to this value
class Lt<T>(value: T) : OrderedCriteria // less than this value
class Lte<T>(value: T) : OrderedCriteria // less than or equal to this value
class Match<T>(value: T) : OrderedCriteria // match the value
// ...
}
fun findBy(createdTime: OrderedCriteria<DateTime>)
fun countBy(createdTime: OrderedCriteria<DateTime>)
In this case we can have just one method for searching and cover all the cases. This solution is just an alternative way, it doesn’t mean the actual approach should be deleted.
Also, we can add sorting
enum class Sorting {
ASC, DESC, NONE
}
sealed class OrderedCriteria<T> {
class Gt<T>(value: T, sort: Sorting = Sorting.NONE) : OrderedCriteria
// ...
}
And we also can add some annotation to method parameters to configure more specific projection if needed.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:27 (12 by maintainers)
Top Results From Across the Web
Build dynamic query with values from search form - Office
This article shows you how to dynamically build criteria for a query string with values from a search form in Microsoft Access.
Read more >JPA Criteria builder for dynamic query - java - Stack Overflow
Using a CriteriaQuery, it is possible to do something like that (using Static JPA Metamodel Classes):
Read more >Writing dynamic SQL queries using Spring Data JPA ...
A dynamic SQL query refers to building a query on the fly before executing it. Not just replacing query parameters by their name...
Read more >Dynamic Query - Liferay Help Center
Like Hibernate criteria, Liferay's dynamic queries are chainable. This means that you can add criteria to, set projections on, and add orders to...
Read more >dynamic query with criteria - Forums - Liferay
Hi All, I am trying to implement search query using dynamic query. I created dynamic query like this DynamicQuery dynamicQuery=DynamicQueryFactoryUtil.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@raderio Thanks for the feedback, yes a DSL like that would probably be nice and something we can consider in the future. We certainly need a “Criteria”-like API to allow for dynamic queries as well as static queries.
We first want to get the initial support for static queries stable and released however, so this one is definitely on the radar for the future.
This approach is more flexible than Spring Data JPA, where the query is hard-coded in the method name. So if you need to filters entities by created time in some case greater than and in some case less than, we need to have 2 methods, and choose one of depending on that user sends to us.
That I am proposing is to generate a method at compile time that will create the query at run-time based on the parameters passed to this method. In this case, a lot of boilerplate will be eliminated.