Part of the problem is that if you store a Hibernate lazy POJO in your page as 
a persistent property, then the sesison will have been closed by the time you 
go to later use it (in a later request-response cycle).

One trick I use is to have a special property persistence strategy source that 
reattaches persistent objects when they are requested. (This class assumes 
you already have the Spring hibernate session filter specified in web.xml, 
which opens a hibernate sesison for the length of a request-response cycle, 
and binds it to the current thread).

public class HibernatePropertyPersistenceStrategySourceImpl extends
        PropertyPersistenceStrategySourceImpl {

    @Override
    public Collection getAllStoredChanges(String pageName) {
        Collection c = super.getAllStoredChanges(pageName);
        
        for (Iterator i = c.iterator(); i.hasNext();){
            PropertyChange pc = (PropertyChange) i.next();
            attach(pc.getNewValue());
        }
        return c;
    }

    
    /**
     * Attachs a loaded object to the current thread session, if not already
     * 
     * @param obj
     */
    protected void attach(Object obj){
        Session session = getSession();
        
        if (obj instanceof PersistentObject) {
            PersistentObject po = (PersistentObject) obj;
            if (po.isPersistent())
                session.lock(po, LockMode.NONE);
        }
        else if (obj instanceof Collection) {
            for (Iterator i = ((Collection) obj).iterator(); i.hasNext();){
                attach(i.next());
            }
        }
    }
    
    protected Session getSession() {
        return SessionFactoryUtils.getSession(getSessionFactory(), false);
    }

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

and in hivemodule.xml (this assumes you are using the spring integration jar):

  <!-- Override PropertyPersistenceStrategySource implementation with 
hibernate-specific one -->    
  <implementation 
service-id="tapestry.persist.PropertyPersistenceStrategySource">

    <invoke-factory>
      <construct 
class="com.itglovebox.tapestry.record.hib.HibernatePropertyPersistenceStrategySourceImpl">
        <set-configuration property="contributions" 
configuration-id="tapestry.persist.PersistenceStrategy"/>
        <set-object property="sessionFactory" value="spring:sessionFactory"/>
      </construct>
    </invoke-factory>
    
  </implementation>  


Hope this helps.

regards,
Scott

On Mon 19 December 2005 12:24, Cosmin Bucur wrote:
> I've also posted this on the spring forum , as I think it is sort of a
> mixed problem . I figured that maybe other tapestry/spring/hibernate
> users here might have some insight :
>
> I am using latest spring , hibernate 3 and tap 4 . I have the default
> true setting for lazy loading in hibernate . I would like to take
> advantage of this , from within the tapestry 4 layers .
>
> I am getting the : no session or session closed error .
>
> I've searched the forum , and came across a few good posts aslo a good
> article : http://www.jroller.com/comments/kbau...ation_with_dao
>
> However if i'm understanding the article right , it's talking about
> having an interceptor aplied to a spring business object that
> initialises the session when needed ?
>
>  ... that does me no good when trying to access a lazy collection from
> a tapestry Page or Component .
>
> Is there a common fix for this scenario also , or am I just
> understanding this wrong ? ... can I maybe set the tap Page as a
> spring bean and put the intercecptor on it ?
>
> Anticipated thanks
> Cosmin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to