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.

Dynamic query criteria

See original GitHub issue

At 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:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:27 (12 by maintainers)

github_iconTop GitHub Comments

15reactions
graemerochercommented, Sep 24, 2019

@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.

3reactions
raderiocommented, Aug 17, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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