Grails
  1. Grails
  2. GRAILS-7324

Criteria: ordering by existing association property causes org.hibernate.QueryException

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.7
    • Fix Version/s: 2.0-M2
    • Component/s: Persistence
    • Labels:
      None

      Description

      Sample code:

      MyEntity.createCriteria().list(sort:'associatedObject.name',order:'asc') {
          associatedObject {
              ....
          }
      }
      

      This will cause org.hibernate.QueryException: duplicate association path: associatedObject.
      I think its caused by the code in GrailsHibernateUtil.addOrderPossiblyNested: it doesn't reuse existing alias and always tried to create a new one.

      1. AssociatedEntity.groovy
        0.1 kB
        Serge P. Nekoval
      2. RootEntity.groovy
        0.2 kB
        Serge P. Nekoval
      3. RootEntityController.groovy
        0.3 kB
        Serge P. Nekoval

        Activity

        Hide
        Graeme Rocher added a comment -

        attach an example that reproduces the issue

        Show
        Graeme Rocher added a comment - attach an example that reproduces the issue
        Hide
        Serge P. Nekoval added a comment -

        I've generated an application and attached 3 files for reproducing the issue.
        Running RootEntityController/index will cause:

        org.hibernate.QueryException: duplicate association path: association
        	at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1156)
        	at grails_7324.RootEntityController$_closure1.doCall(RootEntityController.groovy:6)
        	at grails_7324.RootEntityController$_closure1.doCall(RootEntityController.groovy)
        	at java.lang.Thread.run(Thread.java:662)
        
        Show
        Serge P. Nekoval added a comment - I've generated an application and attached 3 files for reproducing the issue. Running RootEntityController/index will cause: org.hibernate.QueryException: duplicate association path: association at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1156) at grails_7324.RootEntityController$_closure1.doCall(RootEntityController.groovy:6) at grails_7324.RootEntityController$_closure1.doCall(RootEntityController.groovy) at java.lang. Thread .run( Thread .java:662)
        Hide
        Serge P. Nekoval added a comment -

        I've made a fix against 1.4, however its easy to port it to 1.3.x branch as well.
        Here is my pull request on github: https://github.com/grails/grails-core/pull/72

        Show
        Serge P. Nekoval added a comment - I've made a fix against 1.4, however its easy to port it to 1.3.x branch as well. Here is my pull request on github: https://github.com/grails/grails-core/pull/72
        Hide
        Silaghi Paula added a comment -

        I still get the error: duplicate association path: collection1 for grails 2.0.3. Here is my code:

        PagedResultList<X> result= X.createCriteria().list(max: 1, offset: 100) {
        collection1

        {...}
        createAlias("collection1", "c1")
        createAlias("c1.collection2", "c2")
        command.sort = 'c2.date';
        order(command.sort, "asc")
        }

        if I remove collection1{...}

        the query works fine and the sorting is done correctly. Is there any other way to sort on 2 inner properties, like collection1.collection2.date ?

        Show
        Silaghi Paula added a comment - I still get the error: duplicate association path: collection1 for grails 2.0.3. Here is my code: PagedResultList<X> result= X.createCriteria().list(max: 1, offset: 100) { collection1 {...} createAlias("collection1", "c1") createAlias("c1.collection2", "c2") command.sort = 'c2.date'; order(command.sort, "asc") } if I remove collection1{...} the query works fine and the sorting is done correctly. Is there any other way to sort on 2 inner properties, like collection1.collection2.date ?
        Hide
        Serge P. Nekoval added a comment -

        Any reason why you use createAlias("collection1", "c1") instead of just using collection1?
        My understanding is that createAlias() is a method of Criteria, not CriteriaBuilder. For Criteria DSL the documented way is either using join "collection1" or nested closure.

        Show
        Serge P. Nekoval added a comment - Any reason why you use createAlias("collection1", "c1") instead of just using collection1 ? My understanding is that createAlias() is a method of Criteria, not CriteriaBuilder. For Criteria DSL the documented way is either using join "collection1" or nested closure.

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Serge P. Nekoval
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development