4 Replies Latest reply on Oct 29, 2007 9:02 AM by zaya

    Eager loading in EntityQuery

    zaya

      I'm wondering if this is possible. I'm using EntityQuery in Seam Application Framework.

      Say, I want to page query for Comment entity, but when I show comments for an article, I need the name of the user who contributed this comment, and this information is in my User entity.

      The consequence is n+1 queries. How to avoid this?

        • 1. Re: Eager loading in EntityQuery

          Have you tried this?


          SELECT c FROM Comment c
          INNER JOIN FETCH c.user
          


          Regards

          Felix

          • 2. Re: Eager loading in EntityQuery
            zaya

            It gave such error:

            org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
            


            • 3. Re: Eager loading in EntityQuery
              zaya

              I read Hibernate doc, and it says:

              Nor should fetch be used together with setMaxResults() or setFirstResult() as these operations are
              based on the result rows, which usually contain duplicates for eager collection fetching, hence, the number of
              rows is not what you'd expect.
              


              EntityQuery used paging, there's certainly setMaxResults().

              I'm kinda confused here. I executed generated mysql from mysql log, and the result seemed correct. Just the transaction was rolled back.

              • 4. Re: Eager loading in EntityQuery
                zaya

                When I specify ejbql="select c from Comment c inner join fetch c.user" in components.xml, the generated ejbql is correct, but to retrieve the count number, it generated "select count(*) from Comment inner join fetch c.user where ......", that's wrong.

                There isn't setCountEjbql() method either in EntityQuery or in Query. I have to extend the class. And because there is WHERE clause appended, I too have to recalculate the String, which is annoying and redundant.

                Created Jira issue:
                http://jira.jboss.com/jira/browse/JBSEAM-2177

                And thanks fhh, I think you're correct.