Menu

Struts-polls V2.2 - OSV question

2003-06-24
2003-06-25
  • Peter Rutner

    Peter Rutner - 2003-06-24

    Hi,

    I have a question regarding your DAO - OSV implementation. I was thouroughly reading your code, and found it curious that in the "ListResults.java" action the collection returned from the "getDAO().getResults();" call is not initialized, but passed back to the JSP, AFTER the hibernate session is closed! This would lead to a empty or null collection in the JSP!

    I had a look at the hibernate mapping file and found that the 'set' is set to be lazy initialized. I feel that it can't work because the collection on the JSP can't be lazy initialzed since the hibernate session is already closed. Did I miss something? Any tip or enlighting is very appricated.

    thx
    Peter

     
    • Jorge de Castro

      Jorge de Castro - 2003-06-25

      Hmmm, that's interesting.

      First of all, I don't think my code is an OSV implementation yet (despite me bragging about it on the class names and release files -its more of a preparation for it)
      After all, I am *not* closing the session _after_ each request or after the execution of each execute method action.
      It seems to me that I am simply moving the try/finally code that closes the session from the DAO methods into the actions themselves.
      Since I am not sure about it, I would also enjoy
      comments on this -maybe I'll ask Gavin.

      What I should be doing is a try/finally "around" each execute action (would have to be in a class that dispatches actions), but that would take some time so I left it for the next release.

      Now regarding your question, I take it lazy refers to the class associations, ie, if set to true hibernate won't fetch the associations unless you explicitly require so. So if you get all polls, unless you specify it explicitly, hibernate won't join the poll results in the err final result.

      On ListResults, since I'm not using proxies (which attain the same goal), a query for all results joins with the corresponding poll -that is the bit you saw.

      So if I was to invoke poll.getResults() on a JSP I'd get an empty/null set because of the lazy initialization.

      Since I mapped as bi-directional 1-2-many, if on the reverse association I wanted the same behavior to occur, I'd need a proxy on the poll mapping.

      I'm on holiday and I don't have the code at hand, but I'm almost sure that is the case.
      Hope this somehow helps you.

      Cheers
      jorge

       
    • Jorge de Castro

      Jorge de Castro - 2003-06-25

      Perhaps I should be a bit more specific:

      >>  had a look at the hibernate mapping file and found that the 'set' is set to be lazy initialized.<<

      Correct. This means when I retrieve polls I will not retrieve and initialize the associated set of results by default.

      >> in the "ListResults.java" action the collection returned from the "getDAO().getResults();" call is not initialized, but passed back to the JSP, AFTER the hibernate session is closed! <<

      There is a 1-2-many association between polls and poll-results. Evidently, 1 poll may have many results (or a collection of results). ListResults does exactly what the name suggests. However, since no lazy loading was specified for this end of the association (by means of a proxy on polls) it DOES initialize the associated entity.
      The lazy flag is set for the collection associated with polls, so when I fetch polls, no poll-results will be initialized implicitly.

      Nothing is "passed back after the session is closed" because the session is closed on a finally block, when and after everything executes.

      This bit is quoted from myself
      >> I take it lazy refers to the class associations <<

      The way I see it is if you have collections associated with an entity, you use the lazy flag to lazy load the ditto. If you're coming from the other end of the association and you also want to lazy-load, you use proxies.

      Now I am sure there are many possible improvements to my handling of hibernate, since I am new to it. Hopefully, my next version will be simpler and better.

      Hope that helps.

      jorge

       

Log in to post a comment.