Grails
  1. Grails
  2. GRAILS-9829

Eager fetching does not work as expected with FetchMode.JOIN

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.1.3, 2.2
    • Fix Version/s: None
    • Component/s: Persistence
    • Labels:
    • Environment:
      NA

      Description

      A criteria query in a Grails service class where an eager join is expected via FetchMode.SELECT or FetchMode.EAGER (to avoid lazy loading of child objects), the query executes as expected, but when crawling the association in a view, Hibernate is executing subsequent queries as if it were lazily loading the associations.

      For more details, look at the attached sample app or see original question on StackOverFlow: http://stackoverflow.com/questions/14758499/grails-gorm-criteria-query-eager-fetching

      Sample application demonstrates the issue by disabling the Hibernate OSIV and a "org.hibernate.LazyInitializationException" happens when visiting: http://localhost:8080/GrailsEagerFetchingProblem/market/test

        Activity

        Hide
        Graeme Rocher added a comment -

        This is yet another Hibernate bug https://hibernate.onjira.com/browse/HHH-3524

        Show
        Graeme Rocher added a comment - This is yet another Hibernate bug https://hibernate.onjira.com/browse/HHH-3524
        Hide
        Alex Beardsley added a comment -

        Is there some sort of fix/workaround for this? It's terribly annoying. That hibernate bug has been open since 2008 so I doubt it is getting fixed anytime soon.

        Show
        Alex Beardsley added a comment - Is there some sort of fix/workaround for this? It's terribly annoying. That hibernate bug has been open since 2008 so I doubt it is getting fixed anytime soon.
        Hide
        Graeme Rocher added a comment -

        There are two current workarounds:

        a) use LEFT_JOIN in the criteria query

        b) use HQL instead of criteria

        Show
        Graeme Rocher added a comment - There are two current workarounds: a) use LEFT_JOIN in the criteria query b) use HQL instead of criteria
        Hide
        Graeme Rocher added a comment - - edited

        a) can be done with (where class Foo has a 'bars' association):

        Foo.withCriteria {
          bars(CriteriaSpecification.LEFT_JOIN) {
             eq 'a', 'b'
          }
        }
        
        Show
        Graeme Rocher added a comment - - edited a) can be done with (where class Foo has a 'bars' association): Foo.withCriteria { bars(CriteriaSpecification.LEFT_JOIN) { eq 'a', 'b' } }
        Hide
        Donal Murtagh added a comment -

        This workaround did not work for me, I don't get any Foos returned when there are no associated Bars

        Show
        Donal Murtagh added a comment - This workaround did not work for me, I don't get any Foos returned when there are no associated Bars

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Bobby Warner
          • Votes:
            8 Vote for this issue
            Watchers:
            13 Start watching this issue

            Dates

            • Created:
              Updated:

              Development