Skip to content

Grails 3.3.0 findAllBy* in bidirectional hasMany relation is producing error. #14600

Open

Description

@mdaynul

I was trying to use addTo* method, then findAllBy*, it is producing unexpected error.

GORM dynamic finder should not suggest such methods -
Book.findAllByAuthors() if it does not work.

I posted the details in Stackoverflow,

class Author {

    String name

    static hasMany = [fiction: Book, nonFiction: Book]
}
class Book {
    String title

    static belongsTo = Author
    static hasMany = [authors:Author]
}
class BookController {
    def testBook(){
        def fictBook = new Book(title: "IT")
        def fictBook2 = new Book(title: "MBA")
        def fictBook3 = new Book(title: "DBA")
        def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
        def nonFictBook2 = new Book(title: "Cleaning Codex writer")
        def a = new Author(name: "Stephen King")
                .addToFiction(fictBook)
                .addToNonFiction(nonFictBook)
                .save(flush:true)

        println a.fiction
        // println Book.findAllByAuthors([a])   // doesn't work, same error
        println Book.withCriteria() {
            'in'('authors', [a])
        }

       // it works
       // println Book.withCriteria() {
       //  authors {
       //        'in'('id', [a.id])
       //   }
       // }

        render "helllo"
    }
}

Expected Behaviour

Book.findAllByAuthors() should give the list of the books from given Authors

Actual Behaviour

URI /book/testBook
Class org.h2.jdbc.JdbcSQLException
Message null
Caused by Parameter "#1" is not set; SQL statement: select this_.id as id1_4_0_, this_.version as version2_4_0_, this_.title as title3_4_0_ from book this_ where this_.id in (?) [90012-195]

Environment Information

  • Operating System: Ubuntu 17.04
  • Grails Version: 3.3.0
  • JDK Version: 1.8.0_131
  • gormVersion: 6.1.6.RELEASE
  • gradleWrapperVersion: 3.5

Example Application

  • TODO: link to github repository with example that reproduces the issue

Activity

  1. changed the title [-]Grails 3.3.0 bidirectional hasMany is not working[/-] [+]Grails 3.3.0 findAllBy* in bidirectional hasMany relation is producing error.[/+] on Nov 6, 2017
  2. mdaynul commented on Nov 6, 2017

    @mdaynul
    Author

    Stacktrace -

    2017-11-06 20:22:02.441 ERROR --- [nio-9090-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : Parameter "#1" is not set; SQL statement:
    select this_.id as id1_4_0_, this_.version as version2_4_0_, this_.title as title3_4_0_ from book this_ where this_.id=? [90012-195]
    2017-11-06 20:22:02.458 ERROR --- [nio-9090-exec-4] o.g.web.errors.GrailsExceptionResolver   : JdbcSQLException occurred when processing request: [GET] /book/testBook
    Parameter "#1" is not set; SQL statement:
    select this_.id as id1_4_0_, this_.version as version2_4_0_, this_.title as title3_4_0_ from book this_ where this_.id=? [90012-195]. Stacktrace follows:
    
    java.lang.reflect.InvocationTargetException: null
            at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
            at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
            at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
            at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
            at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
            at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
            at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:54)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:150)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:64)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
            at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
            at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
            at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
            at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:748)
    Caused by: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
            at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
            at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
            at org.hibernate.loader.Loader.getResultSet(Loader.java:2122)
            at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1905)
            at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
            at org.hibernate.loader.Loader.doQuery(Loader.java:925)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
            at org.hibernate.loader.Loader.doList(Loader.java:2622)
            at org.hibernate.loader.Loader.doList(Loader.java:2605)
            at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
            at org.hibernate.loader.Loader.list(Loader.java:2429)
            at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
            at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
            at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
            at org.grails.orm.hibernate.query.AbstractHibernateQuery.listForCriteria(AbstractHibernateQuery.java:716)
            at org.grails.orm.hibernate.query.AbstractHibernateQuery.list(AbstractHibernateQuery.java:706)
            at org.grails.datastore.gorm.finders.FindAllByFinder.invokeQuery(FindAllByFinder.java:54)
            at org.grails.datastore.gorm.finders.FindAllByFinder$1.doInSession(FindAllByFinder.java:48)
            at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:319)
            at org.grails.datastore.gorm.finders.AbstractFinder.execute(AbstractFinder.java:42)
            at org.grails.datastore.gorm.finders.FindAllByFinder.doInvokeInternal(FindAllByFinder.java:45)
            at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:252)
            at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:390)
            at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:185)
            at org.grails.datastore.gorm.GormEntity$Trait$Helper.staticMethodMissing(GormEntity.groovy:756)
            at ovr.BookController$$EQaCwIgC.testBook(BookController.groovy:25)
            ... 37 common frames omitted
    Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
    select this_.id as id1_4_0_, this_.version as version2_4_0_, this_.title as title3_4_0_ from book this_ where this_.id=? [90012-195]
            at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
            at org.h2.message.DbException.get(DbException.java:179)
            at org.h2.message.DbException.get(DbException.java:155)
            at org.h2.expression.Parameter.checkSet(Parameter.java:81)
            at org.h2.command.Prepared.checkParameters(Prepared.java:164)
            at org.h2.command.CommandContainer.query(CommandContainer.java:112)
            at org.h2.command.Command.executeQuery(Command.java:201)
            at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:111)
            at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
            ... 61 common frames omitted
    
    
  3. jeffscottbrown commented on Nov 6, 2017

    @jeffscottbrown
    Contributor

    GORM dynamic finder should not suggest such methods -
    Book.findAllByAuthors() if it does not work.

    Are there examples in the docs that suggest that should work? Thanks for the feedback.

  4. jeffscottbrown commented on Nov 6, 2017

    @jeffscottbrown
    Contributor

    Not a duplicate but related to #10796.

  5. mdaynul commented on Nov 6, 2017

    @mdaynul
    Author

    Thanks for looking into this.

    Grails Docs has no mention about such dynamic methods like Book.findAllByAuthors(...). Also, no mention about the dynamic finder with "many" part of the relation will/may produce SQL errors, (within a many-to-many relation).

    in GGTS/IntelliJ idea, i found this suggestion pops up as always. I checked and found in Grails - 2.5.1, 3.1.7 and 3.3.1

    Again, there is no error like "method not found" when using this "findAllByAuthors(..)"

    So, appearntly, this dynamic method exists but gives SQL errors.

  6. jeffscottbrown commented on Nov 6, 2017

    @jeffscottbrown
    Contributor

    in GGTS/IntelliJ idea, i found this suggestion pops up as always.

    FYI... Grails doesn't have any direct control over what the IDE suggests. The IDEs have their own algorithms for guessing what would be considered valid dynamic finders.

  7. jamesfredley commented on Feb 26, 2026

    @jamesfredley
    Contributor

    This issue has been verified as fixed in Grails 7.x / GORM 8.x.

    We wrote a reproduction test that exercises the bidirectional hasMany scenario with withCriteria and 'in' operator on the association, and it passes on the current 7.0.x codebase.

    If you are still experiencing this issue on Grails 7.x, please reopen with a minimal reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions