I use a custom persistence strategy for this:

http://www.carmanconsulting.com/svn/public/tapernate/trunk/src/java/com/carm
anconsulting/tapernate/record/EntityPropertyPersistenceStrategy.java

Basically, I copied the session persistence strategy implementation and
modified it a bit to actually just store the id of the persistent object in
the session.  

-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of Hans L
Sent: Friday, April 21, 2006 1:29 PM
To: tapestry-user@jakarta.apache.org
Subject: tapestry/hibernate sessions & pageBeginRender()

Hi,

I'm having some difficulty with trying to reattach Tapestry-persisted 
entities to Hibernate sessions.  My main problem appears to be that I 
need a "page-begin-processing" method to hook into that is earlier in 
the request/response lifecycle than pageBeginRender().

I have done some searching on this matter, and while I've seen many 
different wiki pages and posts that address similar concerns, I have not 
found a solution for this problem yet.  I apologize if I've overlooked 
something.

I am using the pageBeginRender() method to reattach a persist="session" 
object to the Hibernate Session:

public void pageBeginRender(PageEvent event)
{       
                
        SessionFactory sf = getContactDao().getSessionFactory();
        Session s = sf.getCurrentSession();
        s.update(getContact()); // contact is persisted property
}       

This works fine when I am submitting the form, since the Contact entity 
is reattached when the form rewinds -- and before my doStoreContact() 
method is executed.

The problem is that I am using the same page to add/remove PhoneNumber 
entities that are related to the Contact entity.  Because the 
PhoneNumber objects are stored in a Set in the entity, I want to 
reattach the Contact and then remove the PhoneNumbers from the Set (and 
cascading should take care of the rest).  I created a 
doRemovePhoneNumber() method, which is invoked by a DirectLink component 
on the form page:

public void doRemovePhoneNumber(Integer id)
{
        IContactDao = getContactDao(); // spring-injected
        getContact().getPhoneNumbers().remove(dao.get(PhoneNumber.class,
id));
        dao.save(getContact());  // saveOrUpdate()
}

Unfortunately, when I invoke that method, I get an exception thrown from 
my beginPageRender() method:

org.hibernate.NonUniqueObjectException: a different object with the same 
identifier value was already associated with the session: [Contact#1]

When I looked into the matter, I noticed that pageBeginRender() is not 
being invoked before the doRemovePhoneNumber() method, but only after it 
has finished (and before the page rendered).  While this makes sense, it 
creates a problem for me because my pageBeginRender() method is trying 
to reattach the object.  I would really like to re-attach the object 
immediately and then proceed to perform any operations on it.

So, is there a method that is invoked at the beginning of every 
processing request that I could hook into to reattach my entity?  Are 
other people also dealing with similar issues in a better way?

Thanks in advance for any help -

Hans


---------------------------------------------------------------------
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