Announcement Announcement Module
Collapse
No announcement yet.
Lazy load problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Order.java
    OrderItem.java
    ...
    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.

    OrderDao.java & OrderDaoHbm.java: interface and implements
    OrderItemDao.java & OrderItemDaoHbm.java

    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():
    Code:
    class DAFacade{
       ...
       List getOrderItemsByOrder(OrderId){
          getHibernateTemplate().....
       }
       ...
    }
    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!

    Comment


    • #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.

      Comment


      • #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.

        Code:
         
              public List findPlaceByNameAndMarks(final String name) throws DataAccessException {
        	return getHibernateTemplate().findByNamedParam(
        						       "from nz.org.geonet.rln.domain.Place as place "
        						       + "inner join fetch place.marks "
        						       + "where place.name = :name",
        						       "name",
        						       name
        						       );
        and then

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

        See http://www.hibernate.org/hib_docs/re...queryhql-joins

        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.

        -Geoff

        Comment

        Working...
        X