grails / gorm-hibernate5

GORM for Hibernate 5
Apache License 2.0
66 stars 71 forks source link

Executing DetachedCriteria with subcriteria multiple times fails #182

Closed timaebi closed 4 years ago

timaebi commented 4 years ago

Background

For a filtered view with pagination we use a DetachedCriteria which specifies all data to be returned. We then run criteria.count() and criteria.list(params) to receive a page. However this fails, if the criteria contains a detached criteria as subcriteria.

Steps to Reproduce

Have a look at the attached project (gorm-bug-report) which describes the error. There is one integration test, which currently fails but shouldn't in our opinion. The project setup was done in a straight forward manner based on grails 3.3.11:

  1. grails create-app gorm-bug-report
  2. create domain classes User and Organisation with a many-to-one relationship
  3. create a test
  4. run ./gradlew integrationTest and see the test failing

Expected Behaviour

Executing the same DetachedCriteria multiple times should return the same results and not throw an exception.

Actual Behaviour

The first execution of the query works well, however a second call throws the following exception:

Expected no exception of type 'org.springframework.core.convert.ConverterNotFoundException' to be thrown, but got it nevertheless
    at spock.lang.Specification.notThrown(Specification.java:107)
    at AssociationSubCriteriaSpec.$tt__$spock_feature_0_0(AssociationSubCriteriaSpec.groovy:49)
    at AssociationSubCriteriaSpec.test run detached criteria with association criteria multiple times_closure2(AssociationSubCriteriaSpec.groovy)
    at groovy.lang.Closure.call(Closure.java:420)
    at groovy.lang.Closure.call(Closure.java:436)
    at grails.gorm.transactions.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:68)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at grails.gorm.transactions.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:65)
    at AssociationSubCriteriaSpec.test run detached criteria with association criteria multiple times(AssociationSubCriteriaSpec.groovy)
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.hibernate.criterion.DetachedCriteria] to type [bug.report.Organisation]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:206)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187)
    at org.grails.orm.hibernate.query.AbstractHibernateQuery.doTypeConversionIfNeccessary(AbstractHibernateQuery.java:277)
    at org.grails.orm.hibernate.query.AbstractHibernateQuery.add(AbstractHibernateQuery.java:170)
    at org.grails.datastore.gorm.finders.DynamicFinder.applyDetachedCriteria(DynamicFinder.java:601)
    at grails.gorm.DetachedCriteria.withPopulatedQuery_closure8(DetachedCriteria.groovy:756)
    at groovy.lang.Closure.call(Closure.java:420)
    at groovy.lang.Closure.call(Closure.java:436)
    at org.grails.datastore.gorm.GormStaticApi.withDatastoreSession_closure24(GormStaticApi.groovy:862)
    at groovy.lang.Closure.call(Closure.java:420)
    at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:319)
    at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:40)
    at org.grails.datastore.gorm.GormStaticApi.withDatastoreSession(GormStaticApi.groovy:861)
    at grails.gorm.DetachedCriteria.withPopulatedQuery(DetachedCriteria.groovy:740)
    at grails.gorm.DetachedCriteria.list(DetachedCriteria.groovy:135)
    at grails.gorm.DetachedCriteria.list(DetachedCriteria.groovy:134)
    at AssociationSubCriteriaSpec.$tt__$spock_feature_0_0(AssociationSubCriteriaSpec.groovy:46)
    ... 7 more

Environment Information

Example Application

The gorm-bug-report repository contains an integration test, which fails because of the bug.

Related Issues

This issue might be related to grails/grails-data-mapping#59 .

scottlollman commented 2 years ago

This fix seems to miss my scenario. I'm getting crashes from AbstractHibernateCriterionAdapter.applySubCriteriaToJunction calling AbstractHibernateQuery.doTypeConversionIfNeccessary resulting in No converter found capable of converting from type [org.hibernate.criterion.DetachedCriteria] to type [my.DomainClass]