7 Replies Latest reply on Feb 2, 2007 2:37 AM by Andrew Dale

    Query

    Dalibor Kalna Newbie

      Hello people,

      is it possible to fetch collection even if it's annonated as LAZY?
      I know that it will be fetched when I call collection.size() but is
      it possible to set some query parameter to do this automaticaly?

      Thanks a lot
      Dalibor

        • 1. Re: Query
          Andrew Dale Newbie

          Hi,

          In an EJBQL query you can fetch the collection by using a fetch join, such as the following example (taken from Enterprise Java Beans 3.0)

          SELECT c FROM Customer c LEFT JOIN FETCH c.phones


          Where Customer is an entity containing a collection (relationship) of phones.


          Cheers,

          Andy

          • 2. Re: Query
            Dalibor Kalna Newbie

            Hi Andy,

            thanks a lot! It works! Now, how would you create SQL statement when every Phone will have yet another collection ?

            Customer -> Collection(Phone) -> Collection(Something)

            Thanks
            Dalibor

            • 3. Re: Query
              Andrew Dale Newbie

              It is possible to keep fetching ahead, but you need to watch out for the classic hibernate "Could not fetch multiple bags" exception. To do multiple fetching, you will just need to use aliases, for example:

              SELECT c FROM Customer c LEFT JOIN FETCH c.phones cp JOIN FETCH cp.<some other collection>


              Cheers,

              Andy

              • 4. Re: Query
                Dalibor Kalna Newbie

                Thanks Andy,

                well, and how to avoid this multiple bags exception thing? I have read that I should use Set instead of generic Collection, but is this solution?

                Bye
                Dalibor

                • 5. Re: Query
                  Andrew Dale Newbie

                  Hi,

                  The are a few ways that the multiple bag exception can be avoided.

                  You mentioned using a Set instead, this would solve the problem but it has the restriction that it can only contain distinct values so you need to consider if they will work for you.

                  Another solution would be to use Lists, but i would personally never do this as i have experienced lots of problems (like them missing out the join table they create in queries) with Bi-Directional indexed relationships (using a list to map a oneToMany)

                  The thrid solution could be to keep using collections, but also annotate with @CollectionId, i have not used this yet, but i reckon it could also run into problems with bi-directional relationships.

                  Andy

                  • 6. Re: Query
                    Dalibor Kalna Newbie

                    Hi Andy,

                    thanks a lot for your help. I really appreciate it.

                    Now, the Set solution seems to be most clean, but in stage of our project
                    I simply can't change this anymore...

                    Well, the third option @CollectionId, is this pure EJB3.0 Annotation or is it part of Hibernate?

                    Thanks
                    Dalibor

                    • 7. Re: Query
                      Andrew Dale Newbie

                      Hi Dalibor,

                      The help is no problem, as i ran into quite a lot of these errors when i first started using Hibernate + EJB3 + JBoss.

                      I also went with the Set to solve the "Multiple Bag" problem, as it seems the easiest so long as it fits the data model. I think @CollectionId is a hibernate specific annotation that is not in the spec, as it was only introduced with the last release.

                      Cheers,

                      Andy