micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

AbstractCriteriaBuilder#function() not Supported

Open elzayakm opened this issue 1 year ago • 16 comments

Feature description

I would like to be able to build queries with JSON functions supported by my DB with Micronaut's QueryBuilder.

My repository implements the CoroutineJpaSpecificationExecutor and is configured to use the JpaQueryBuilderclass, as shown in the code snippet below.

@Repository
@RepositoryConfiguration(queryBuilder = JpaQueryBuilder::class)
interface ScheduledUpdateRepository : CoroutinePageableCrudRepository<X, Long>, CoroutineJpaSpecificationExecutor<X>

To do this, I need the function() method which is obviously not supported yet as shown in the code snippet below taken from the AbstractQueryBuilder class.

    @Override
    @NonNull
    public <T> Expression<T> function(@NonNull String name, @NonNull Class<T> type, @NonNull Expression<?>... args) {
        throw notSupportedOperation();
    }

If any additional info is required, please reach out to me on [email protected]. Thanks!

elzayakm avatar Dec 15 '23 15:12 elzayakm

@dstepanov is it possible that this ticket gets prioritized? My team is building a feature that will highly depend on this method.

elzayakm avatar Dec 19 '23 10:12 elzayakm

Sure, it should use Hibernate's criteria builder in this case.

dstepanov avatar Dec 19 '23 10:12 dstepanov

Do you have an example on how to use it? I am not able to get my Micronaut app configured to use it instead of the RuntimeCriteriaBuilder.

elzayakm avatar Dec 19 '23 11:12 elzayakm

For now, you can inject JpaRepositoryOperations and use getEntityManagerFactory, getEntityManagerFactory to build queries. I will try to fix it to be used in the repositories for the next release.

dstepanov avatar Dec 19 '23 11:12 dstepanov

Cool then! I prefer to wait for your release so that we don't pollute our code. Thanks a lot for your help!

I will follow-up regarding the status of this within two weeks after the new year. Is that ok?

elzayakm avatar Dec 19 '23 11:12 elzayakm

Yes, looking to fix it in 4.3.0 https://github.com/micronaut-projects/micronaut-core/discussions/10137

dstepanov avatar Dec 19 '23 12:12 dstepanov

Awesome! Thanks a lot!

elzayakm avatar Dec 19 '23 12:12 elzayakm

Hello @dstepanov, reaching out again regarding this. You told me you were looking to fix it in 4.3.0 released today. Still, I see that the method still throws an UnsupportedExcpetion. Are there any updates?

Thank you, and best regards 🙏

kareemelzayat avatar Jan 15 '24 19:01 kareemelzayat

If you use Hibernate it should use the criteria builder from it.

dstepanov avatar Jan 16 '24 11:01 dstepanov

Not sure if that fix is released yet, maybe can try with the 4.4.1-SNAPSHOT?

radovanradic avatar Jan 16 '24 11:01 radovanradic

My repository extends the CoroutineJpaSpecificationExecutor, which allows implementations of the PredicateSpecification and QuerySpecification interfaces only. Those interfaces are always intercepted by the AbstarctSpecificationInterceptor, which I believe does NOT use the HibernateCriteriaBuilderDelegate, rather the AbstractCriteriaBuilder; which is what I am reporting in this ticket.

kareemelzayat avatar Jan 16 '24 11:01 kareemelzayat

Does anybody have a working example of using a different criteria builder? We are not using Hibernate and trying to work with JSONB in postgresql. So I either need to figure out how to use custom operator -> or function jsonb_extract_path

Since we have no Hibernate, the AbstractCriteriaBuilder is the only implementation I see on my class path.

I would very much appreciate a way to call a custom operator or a function. Please help.

Trying to add io.micronaut.data:micronaut-data-jpa library and injecting JpaRepositoryOperations to get a different criteria builder via jpaRepositoryOperations.getEntityManagerFactory().getCriteriaBuilder() only lead to this error:

No bean of type [io.micronaut.data.jpa.operations.JpaRepositoryOperations] exists.

voronaam avatar May 17 '24 22:05 voronaam

I will take a look to support custom functions

dstepanov avatar May 18 '24 12:05 dstepanov

This is pretty bad, even such simple operation like concat, coalesce, lower and upper aren't supported yet by the AbstractCriteriaBuilder. Luckily in my case, I was able to fulfil my need for "case-insensitive like" operation using the ilikeString method, but I believe sooner or later, these operations will be demanded, since not everyone are using Hibernate. I hope these operations will be available soon.

varvay avatar Jun 04 '24 14:06 varvay

I'm working on a query builder rewrite that should enable the quick addition of more criteria features.

Please create an additional issue of what operations are needed to assess what is wanted.

dstepanov avatar Jun 06 '24 11:06 dstepanov

Sure @dstepanov I've opened a feature request issue in #2975.

varvay avatar Jun 07 '24 10:06 varvay