Uploaded image for project: '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
        Hide
        Soltesz Andras added a comment -

        The LEFT_JOIN workaround doesn't work for me either (Hib3, Grails 2.3.9)

        I very much doubt that using HQL could be named as a workaround for such a fundamental problem. If GORM queries cannot handle JOIN fetching then we have a huge problem at hand since practically every non-trivial, real-life query needs it.

        HQL is practically non-composable so it cannot take the place of GORM queries for the majority of the work (or a huge part of the advantages of GORM/Grails is lost). Composing a HQL query by appending HQL clauses is extreme amount of work compared to GORM queries and it should be resorted to in only very limited, extreme cases. Fetch-joins are definitely not a case like this.

        Show
        Soltesz Andras added a comment - The LEFT_JOIN workaround doesn't work for me either (Hib3, Grails 2.3.9) I very much doubt that using HQL could be named as a workaround for such a fundamental problem. If GORM queries cannot handle JOIN fetching then we have a huge problem at hand since practically every non-trivial, real-life query needs it. HQL is practically non-composable so it cannot take the place of GORM queries for the majority of the work (or a huge part of the advantages of GORM/Grails is lost). Composing a HQL query by appending HQL clauses is extreme amount of work compared to GORM queries and it should be resorted to in only very limited, extreme cases. Fetch-joins are definitely not a case like this.
        Hide
        Soltesz Andras added a comment -

        Some extra info: the above LEFT_JOIN sub-criteria always result an INNER JOIN in the resulting SQL query, regardless of the "nullable" property of the association.

        This makes it unusable for nullable associations since it will always remove the entity from the result if the association is null (this is probably why Donal Murthagh never saw Foo entities returning).

        However, it should work for required/non-nullable associations. Still very bad but it may help some people.

        Show
        Soltesz Andras added a comment - Some extra info: the above LEFT_JOIN sub-criteria always result an INNER JOIN in the resulting SQL query, regardless of the "nullable" property of the association. This makes it unusable for nullable associations since it will always remove the entity from the result if the association is null (this is probably why Donal Murthagh never saw Foo entities returning). However, it should work for required/non-nullable associations. Still very bad but it may help some people.

          People

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

            Dates

            • Created:
              Updated:

              Development