Announcement Announcement Module
No announcement yet.
Lazy load problem Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy load problem

    I am new to the spring and hibernate. And now I am working on a project based on these two framework. I meet the problem of lazy load. It costs me 3 days, but without success. :x

    The main structure of the project is like this:

    In business layer
    some other beans

    The relation between Order and OrderItems are one - many, so in the order, I maintain a Set 'orderItems' and set the lazyload to true. & interface and implements &

    DAFacade: this class has the instances of DaoHbms(OrderDaoHbm, OrderItemDaoHbm, ...). So when I just use this facade to access the data without touching the Daos.

    In the view layer, when I pass an object Order to the jsp, and show the data in the table by JSTL ${order.orderItems}, It works well because I use the OpenSessionInViewInterceptor.

    But in the controller layer, when I want to use order.getOrderItems(), it tells me 'no open session'.
    So do I have to write a mathod in the DAFacade which does the same thing as the getOrderItems():
    class DAFacade{
       List getOrderItemsByOrder(OrderId){
    so I can get OrderItems by getDAFacade().getOrderItemsByOrder(), or there are some other method to achieve the same goal? Thanks a lot!

  • #2
    I tried many ways, but still no result. :x

    Is there anyone can help me? Many thanks and bow!


    • #3
      Your description is somehow poor, so I' m guessing.
      You are loading an object and the rendring it - it works because of OSIVI.
      But later it looks here as you are trying to use a object from previous session. How do you get this object into your controller? From HttpSession?
      If so, try reattaching it to new session before calling getItems.

      Or maybe you need DeferredClose in your OSIVI.


      • #4
        use HQL

        I wondered about this for a while myself. Now in my buisness facade I write a method to satisfy the use case (I want to be able to use the Spring framework in both web and non web apps so OSIVI won't work). In practice this is usually a simple inner join in the DAO, e.g.

              public List findPlaceByNameAndMarks(final String name) throws DataAccessException {
        	return getHibernateTemplate().findByNamedParam(
        						       "from as place "
        						       + "inner join fetch place.marks "
        						       + "where = :name",
        and then

        Set marks = place.getMarks();
        will work without the no open session problem.


        In addition, a "fetch" join allows associations or collections of values to be initialized along with their parent objects, using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.
        Hope this answers your question.