You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Hans L <ha...@gmail.com> on 2006/04/21 19:28:33 UTC
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Ron Piterman <rp...@gmx.net>.
did you try validate listener ? (assuming you use tapestry 4.0)
http://jakarta.apache.org/tapestry/UsersGuide/events.html
Cheers,
Ron Piterman
Hans L wrote:
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Hans Lellelid <ha...@velum.net>.
Hi Mark,
Thanks for the tip -- that's really helpful to know. I'm also not sure
yet whether my "strategy" is the right one for Hibernate objects, but it
does seem to work consistently as long as I reattach things before I
start modifying them. I'll probably stick with it for now, but hook in
using the method you suggest. I'm hesitant to create my own squeezer or
add any more "magic" to the mix right now (since I'm just starting out
with Tapestry).
Thanks again -
Hans
Mark wrote:
> Hi Hans,
>
> I had a similar problem where pageBeginRender hooked in too late and I
> switch to using pageAttached from the PageAttachListener interface.
> It works the same way as pageBeginRender but seems to be called earlier.
>
> Not sure if it helps you - and more importantly if it is the right way
> to handle Hibernate sessions in the first place, but I figured I'd let
> you know...
>
> MARK
>
>
> Hans L wrote:
>> 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: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Mark <li...@mark-arnold.net>.
Hi Hans,
I had a similar problem where pageBeginRender hooked in too late and I
switch to using pageAttached from the PageAttachListener interface.
It works the same way as pageBeginRender but seems to be called earlier.
Not sure if it helps you - and more importantly if it is the right way
to handle Hibernate sessions in the first place, but I figured I'd let
you know...
MARK
Hans L wrote:
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Ted Steen <te...@gmail.com>.
It would be sweet.. :)
Thanks to HiveMind that should be possible, now I'm just waiting for
people (smarter than me) to do it (better than I could do it)
On 4/23/06, Andreas Bulling <sp...@phoenix.hadiko.de> wrote:
> On 23. Apr 2006 - 16:00:12, Ted Steen wrote:
> | Tapernate looks very promising, but there some solutions to choose from.
> | Honeycomb, HiveMind Utilities and Tapernate (and howards ePluribus
> | project) should merge in to one solution for this tapestry+hibernate
> | problem.
>
> Yes, that's what I meant in my "I have a dream..." posting:
> In the best case we would have a .jar to put in the classpath
> and use out-of-the-box.
>
> Andreas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
--
/ted
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Mark <li...@mark-arnold.net>.
Yes, please - maybe that way I could get at least some of the fuses that
my brain has blown while trying to get started with Tapestry fixed again...
Andreas Bulling wrote:
> On 23. Apr 2006 - 16:00:12, Ted Steen wrote:
> | Tapernate looks very promising, but there some solutions to choose from.
> | Honeycomb, HiveMind Utilities and Tapernate (and howards ePluribus
> | project) should merge in to one solution for this tapestry+hibernate
> | problem.
>
> Yes, that's what I meant in my "I have a dream..." posting:
> In the best case we would have a .jar to put in the classpath
> and use out-of-the-box.
>
> Andreas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
On 23. Apr 2006 - 16:00:12, Ted Steen wrote:
| Tapernate looks very promising, but there some solutions to choose from.
| Honeycomb, HiveMind Utilities and Tapernate (and howards ePluribus
| project) should merge in to one solution for this tapestry+hibernate
| problem.
Yes, that's what I meant in my "I have a dream..." posting:
In the best case we would have a .jar to put in the classpath
and use out-of-the-box.
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Ted Steen <te...@gmail.com>.
Tapernate looks very promising, but there some solutions to choose from.
Honeycomb, HiveMind Utilities and Tapernate (and howards ePluribus
project) should merge in to one solution for this tapestry+hibernate
problem.
On 4/22/06, James Carman <ja...@carmanconsulting.com> wrote:
> Oh, and all of that stuff is publicly available. The spring
> transaction/hibernate support stuff is in a "hivemind" project at JavaForge
> (I'm moving it to honeycomb soon). The only thing that I have put into my
> own SVN repository is the "tapernate" stuff. But, I've talked to Howard
> about putting that into the "tapestry" project at JavaForge as one of the
> subprojects.
>
> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of Hans L
> Sent: Saturday, April 22, 2006 3:14 PM
> To: tapestry-user@jakarta.apache.org
> Subject: Re: tapestry/hibernate sessions & pageBeginRender()
>
> Hi James,
>
> James Carman wrote:
> > Well, if you just store the Person object, you have to reattach it to the
> > current session (which is what my code does).
> >
> > I don't want to store just the id in the session. You need to store the
> > whole object to keep the version information (unless of course you don't
> > need the optimistic locking stuff). You're not supposed to manually set
> the
> > version property on Hibernate-managed entities. Hibernate will do that.
> >
> > I don't use the classes in hivetranse or honeycomb. I have my own classes
> > for dealing with all of this and they work for me. You're welcome to use
> > it. It's all Apache v2.0 licensed. You can do all the transaction
> > demarcation stuff and everything using the Spring syntax. The example
> > application (tapernate-example) shows how it all works. I'm going to be
> > adding my stuff to the honeycomb project once we figure out where it's
> going
> > to live (javaforge vs. sourceforge).
>
> I checked out your repository; this is *really* useful code for me --
> and answers all of the big questions I've had in trying to integrate
> hibernate + tapestry. The property persistence strategy approach looks
> to be the most elegant way to deal with the detached entities and I'm
> going to figure out how to add that persistence strategy to my tapestry
> setup on Monday.
>
> One quick question: is there a reason why your persistence solution
> couldn't extend Spring's HibernateDaoSupport directly? Admittedly, I
> haven't looked at the Hivemind HibernateService class that you're
> extending, but it looks like the important part there is the
> getSession() method, which I assume is the same as HibernateDaoSupport.
> I haven't completely understood what Hivemind provides that Spring
> doesn't (or vice versa), but for now I'm trying to keep my application
> as simple as is feasible and trying to stick to one framework (Spring)
> for the dependency injection / IoC.
>
> Thanks again for this contribution -- and I'm glad to hear that it's
> going to be made more publicly available in the future.
>
> Thanks,
> Hans
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
--
/ted
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
You can directly extend the HibernateDaoSupport if you wish. I just added a
setLog( Log log ) method, so that HiveMind would inject a Log object into my
DAOs. The reason that I want to use HiveMind (besides the fact that I'm a
committer on the HiveMind project) is that that's what Tapestry uses. I see
no reason to use two different DI containers.
-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Hans L
Sent: Saturday, April 22, 2006 3:14 PM
To: tapestry-user@jakarta.apache.org
Subject: Re: tapestry/hibernate sessions & pageBeginRender()
Hi James,
James Carman wrote:
> Well, if you just store the Person object, you have to reattach it to the
> current session (which is what my code does).
>
> I don't want to store just the id in the session. You need to store the
> whole object to keep the version information (unless of course you don't
> need the optimistic locking stuff). You're not supposed to manually set
the
> version property on Hibernate-managed entities. Hibernate will do that.
>
> I don't use the classes in hivetranse or honeycomb. I have my own classes
> for dealing with all of this and they work for me. You're welcome to use
> it. It's all Apache v2.0 licensed. You can do all the transaction
> demarcation stuff and everything using the Spring syntax. The example
> application (tapernate-example) shows how it all works. I'm going to be
> adding my stuff to the honeycomb project once we figure out where it's
going
> to live (javaforge vs. sourceforge).
I checked out your repository; this is *really* useful code for me --
and answers all of the big questions I've had in trying to integrate
hibernate + tapestry. The property persistence strategy approach looks
to be the most elegant way to deal with the detached entities and I'm
going to figure out how to add that persistence strategy to my tapestry
setup on Monday.
One quick question: is there a reason why your persistence solution
couldn't extend Spring's HibernateDaoSupport directly? Admittedly, I
haven't looked at the Hivemind HibernateService class that you're
extending, but it looks like the important part there is the
getSession() method, which I assume is the same as HibernateDaoSupport.
I haven't completely understood what Hivemind provides that Spring
doesn't (or vice versa), but for now I'm trying to keep my application
as simple as is feasible and trying to stick to one framework (Spring)
for the dependency injection / IoC.
Thanks again for this contribution -- and I'm glad to hear that it's
going to be made more publicly available in the future.
Thanks,
Hans
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
Oh, and all of that stuff is publicly available. The spring
transaction/hibernate support stuff is in a "hivemind" project at JavaForge
(I'm moving it to honeycomb soon). The only thing that I have put into my
own SVN repository is the "tapernate" stuff. But, I've talked to Howard
about putting that into the "tapestry" project at JavaForge as one of the
subprojects.
-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Hans L
Sent: Saturday, April 22, 2006 3:14 PM
To: tapestry-user@jakarta.apache.org
Subject: Re: tapestry/hibernate sessions & pageBeginRender()
Hi James,
James Carman wrote:
> Well, if you just store the Person object, you have to reattach it to the
> current session (which is what my code does).
>
> I don't want to store just the id in the session. You need to store the
> whole object to keep the version information (unless of course you don't
> need the optimistic locking stuff). You're not supposed to manually set
the
> version property on Hibernate-managed entities. Hibernate will do that.
>
> I don't use the classes in hivetranse or honeycomb. I have my own classes
> for dealing with all of this and they work for me. You're welcome to use
> it. It's all Apache v2.0 licensed. You can do all the transaction
> demarcation stuff and everything using the Spring syntax. The example
> application (tapernate-example) shows how it all works. I'm going to be
> adding my stuff to the honeycomb project once we figure out where it's
going
> to live (javaforge vs. sourceforge).
I checked out your repository; this is *really* useful code for me --
and answers all of the big questions I've had in trying to integrate
hibernate + tapestry. The property persistence strategy approach looks
to be the most elegant way to deal with the detached entities and I'm
going to figure out how to add that persistence strategy to my tapestry
setup on Monday.
One quick question: is there a reason why your persistence solution
couldn't extend Spring's HibernateDaoSupport directly? Admittedly, I
haven't looked at the Hivemind HibernateService class that you're
extending, but it looks like the important part there is the
getSession() method, which I assume is the same as HibernateDaoSupport.
I haven't completely understood what Hivemind provides that Spring
doesn't (or vice versa), but for now I'm trying to keep my application
as simple as is feasible and trying to stick to one framework (Spring)
for the dependency injection / IoC.
Thanks again for this contribution -- and I'm glad to hear that it's
going to be made more publicly available in the future.
Thanks,
Hans
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Hans L <ha...@gmail.com>.
Hi James,
James Carman wrote:
> Well, if you just store the Person object, you have to reattach it to the
> current session (which is what my code does).
>
> I don't want to store just the id in the session. You need to store the
> whole object to keep the version information (unless of course you don't
> need the optimistic locking stuff). You're not supposed to manually set the
> version property on Hibernate-managed entities. Hibernate will do that.
>
> I don't use the classes in hivetranse or honeycomb. I have my own classes
> for dealing with all of this and they work for me. You're welcome to use
> it. It's all Apache v2.0 licensed. You can do all the transaction
> demarcation stuff and everything using the Spring syntax. The example
> application (tapernate-example) shows how it all works. I'm going to be
> adding my stuff to the honeycomb project once we figure out where it's going
> to live (javaforge vs. sourceforge).
I checked out your repository; this is *really* useful code for me --
and answers all of the big questions I've had in trying to integrate
hibernate + tapestry. The property persistence strategy approach looks
to be the most elegant way to deal with the detached entities and I'm
going to figure out how to add that persistence strategy to my tapestry
setup on Monday.
One quick question: is there a reason why your persistence solution
couldn't extend Spring's HibernateDaoSupport directly? Admittedly, I
haven't looked at the Hivemind HibernateService class that you're
extending, but it looks like the important part there is the
getSession() method, which I assume is the same as HibernateDaoSupport.
I haven't completely understood what Hivemind provides that Spring
doesn't (or vice versa), but for now I'm trying to keep my application
as simple as is feasible and trying to stick to one framework (Spring)
for the dependency injection / IoC.
Thanks again for this contribution -- and I'm glad to hear that it's
going to be made more publicly available in the future.
Thanks,
Hans
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
Well, if you just store the Person object, you have to reattach it to the
current session (which is what my code does).
I don't want to store just the id in the session. You need to store the
whole object to keep the version information (unless of course you don't
need the optimistic locking stuff). You're not supposed to manually set the
version property on Hibernate-managed entities. Hibernate will do that.
I don't use the classes in hivetranse or honeycomb. I have my own classes
for dealing with all of this and they work for me. You're welcome to use
it. It's all Apache v2.0 licensed. You can do all the transaction
demarcation stuff and everything using the Spring syntax. The example
application (tapernate-example) shows how it all works. I'm going to be
adding my stuff to the honeycomb project once we figure out where it's going
to live (javaforge vs. sourceforge).
-----Original Message-----
From: Andreas Bulling [mailto:andreas@phoenix.hadiko.de] On Behalf Of
Andreas Bulling
Sent: Saturday, April 22, 2006 10:49 AM
To: Tapestry users
Subject: Re: tapestry/hibernate sessions & pageBeginRender()
On 22. Apr 2006 - 09:17:49, James Carman wrote:
| 1. Well, here's what would happen. Suppose I want to update a Person
| object on my EditPerson page. I use the "session" persistence strategy
and
| I just store the id of the person. So, I render the EditPage and it shows
a
| form. I edit some values and I submit my request. Suppose someone beat
me
| to the punch, though. While I was sitting there looking at the data from
| this Person object and contemplating what their real first name should be,
| someone else used the edit page to update the exact same Person object.
| When my request comes in, I'll do a Hibernate query to lookup the Person
| object (by id) in the database. I'll set some properties on it and commit
| my changes. Since I got the current values out of the database by
querying
| for the object, my request will work just fine and I will have overwritten
| the changes the other user has done. If I use the "entity" persistence
| strategy (my implementation), I will be updating an object that has the
old
| version number in it. Hibernate will throw an exception and I will not be
| allowed to overwrite the changes the other user has done.
Does this problem still exist if I use the "session" strategy
but persist the whole Person object?
If I understood you correctly this means that your implementation is
currently not complete as the part which saves only the id in the
session is missing, right?! Or did you mean with your third sentence
that you do something like this
@Persist
public abstract Integer getPersonId();
public abstract void setPersonId(Integer personId);
Cheers,
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
On 22. Apr 2006 - 09:17:49, James Carman wrote:
| 1. Well, here's what would happen. Suppose I want to update a Person
| object on my EditPerson page. I use the "session" persistence strategy and
| I just store the id of the person. So, I render the EditPage and it shows a
| form. I edit some values and I submit my request. Suppose someone beat me
| to the punch, though. While I was sitting there looking at the data from
| this Person object and contemplating what their real first name should be,
| someone else used the edit page to update the exact same Person object.
| When my request comes in, I'll do a Hibernate query to lookup the Person
| object (by id) in the database. I'll set some properties on it and commit
| my changes. Since I got the current values out of the database by querying
| for the object, my request will work just fine and I will have overwritten
| the changes the other user has done. If I use the "entity" persistence
| strategy (my implementation), I will be updating an object that has the old
| version number in it. Hibernate will throw an exception and I will not be
| allowed to overwrite the changes the other user has done.
Does this problem still exist if I use the "session" strategy
but persist the whole Person object?
If I understood you correctly this means that your implementation is
currently not complete as the part which saves only the id in the
session is missing, right?! Or did you mean with your third sentence
that you do something like this
@Persist
public abstract Integer getPersonId();
public abstract void setPersonId(Integer personId);
Cheers,
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
1. Well, here's what would happen. Suppose I want to update a Person
object on my EditPerson page. I use the "session" persistence strategy and
I just store the id of the person. So, I render the EditPage and it shows a
form. I edit some values and I submit my request. Suppose someone beat me
to the punch, though. While I was sitting there looking at the data from
this Person object and contemplating what their real first name should be,
someone else used the edit page to update the exact same Person object.
When my request comes in, I'll do a Hibernate query to lookup the Person
object (by id) in the database. I'll set some properties on it and commit
my changes. Since I got the current values out of the database by querying
for the object, my request will work just fine and I will have overwritten
the changes the other user has done. If I use the "entity" persistence
strategy (my implementation), I will be updating an object that has the old
version number in it. Hibernate will throw an exception and I will not be
allowed to overwrite the changes the other user has done.
2. If you want to see the squeezer in action. Use a @Hidden component and
set the value of it to an object which your squeezer supports.
-----Original Message-----
From: Andreas Bulling [mailto:andreas@phoenix.hadiko.de] On Behalf Of
Andreas Bulling
Sent: Saturday, April 22, 2006 9:07 AM
To: Tapestry users
Subject: Re: tapestry/hibernate sessions & pageBeginRender()
| Well, if you just store the id (and entity name) in the session, then you
| totally circumvent the automatic optimistic locking capability of
Hibernate
| (you'd be reading the current version number every time instead of just
| attempting to reattach the old object).
Well, I have to admit that I don't understand what the optimistic locking
feature has to do with the strategy issue :(
>>From my understanding it should be possible to just store the id
to get a smaller session (but more database accesses for sure).
And could you please say what your persistence strategy can be used
for because I don't understand this either - thanks!
| Squeezers aren't used by the client persistence strategy, unfortunately.
OK, and when _are_ they used? With @Persist? How can I see them
"in action"? ;)
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
| Well, if you just store the id (and entity name) in the session, then you
| totally circumvent the automatic optimistic locking capability of Hibernate
| (you'd be reading the current version number every time instead of just
| attempting to reattach the old object).
Well, I have to admit that I don't understand what the optimistic locking
feature has to do with the strategy issue :(
>From my understanding it should be possible to just store the id
to get a smaller session (but more database accesses for sure).
And could you please say what your persistence strategy can be used
for because I don't understand this either - thanks!
| Squeezers aren't used by the client persistence strategy, unfortunately.
OK, and when _are_ they used? With @Persist? How can I see them
"in action"? ;)
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
Well, if you just store the id (and entity name) in the session, then you
totally circumvent the automatic optimistic locking capability of Hibernate
(you'd be reading the current version number every time instead of just
attempting to reattach the old object).
Squeezers aren't used by the client persistence strategy, unfortunately.
-----Original Message-----
From: Andreas Bulling [mailto:andreas@phoenix.hadiko.de] On Behalf Of
Andreas Bulling
Sent: Saturday, April 22, 2006 8:55 AM
To: Tapestry users
Subject: Re: tapestry/hibernate sessions & pageBeginRender()
On 22. Apr 2006 - 08:44:59, James Carman wrote:
| Yes, I'm sorry. I was thinking of my "squeezer" implementation. The
| persistence strategy will store the object in the session, but reattach it
| when necessary. You're right.
Well, OK, but what's the advantage of your entity strategy
in this case? ;)
Do you think it's possible to store only the id of the object in the
session? I see a problem with the types of the different objects
because if you want to reattach the object you have to know
which database table (=which Pojo type) to load from, don't you?
I have another question:
I added a squeezer to my application and wanted to see if it works
as expected. I've set breakpoints in the squeeze and unsqueeze
methods but the code never gets excecuted.
Do I have to change @Persist to @Persist("client") on the
page to see the result of the squeezing?
Thanks!
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
On 22. Apr 2006 - 08:44:59, James Carman wrote:
| Yes, I'm sorry. I was thinking of my "squeezer" implementation. The
| persistence strategy will store the object in the session, but reattach it
| when necessary. You're right.
Well, OK, but what's the advantage of your entity strategy
in this case? ;)
Do you think it's possible to store only the id of the object in the
session? I see a problem with the types of the different objects
because if you want to reattach the object you have to know
which database table (=which Pojo type) to load from, don't you?
I have another question:
I added a squeezer to my application and wanted to see if it works
as expected. I've set breakpoints in the squeeze and unsqueeze
methods but the code never gets excecuted.
Do I have to change @Persist to @Persist("client") on the
page to see the result of the squeezing?
Thanks!
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
Yes, I'm sorry. I was thinking of my "squeezer" implementation. The
persistence strategy will store the object in the session, but reattach it
when necessary. You're right.
> On 21. Apr 2006 - 14:44:59, James Carman wrote:
> | 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.
>
> I've taken a look into your implementation in the meanwhile.
> No offense, but I don't see/understand why you're saying that you only
> store the id of the persistent object in the session:
>
> ---
> session.setAttribute(attributeName, entity == null ? null
> : new DetachedObject(entity, getSession()));
> ---
>
> In DetachedObject you do
>
> ---
> public DetachedObject( Object object, Session session )
> {
> this.object = object;
> reattachRequired = session.getSessionFactory().getClassMetadata(
> object.getClass() )
> .getIdentifier( object, EntityMode.POJO ) != null;
> }
> ---
>
> This means that you save the object in a DetachedObject and this _whole_
> DetachedObject in the WebSession afterwards, don't you?
>
> Wouldn't you have to call object.getId() or something like that in
> DetachedObject and save this in an private Integer instead?
>
> Kind regards,
> Andreas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
James Carman, President
Carman Consulting, Inc.
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
On 21. Apr 2006 - 14:44:59, James Carman wrote:
| 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.
I've taken a look into your implementation in the meanwhile.
No offense, but I don't see/understand why you're saying that you only
store the id of the persistent object in the session:
---
session.setAttribute(attributeName, entity == null ? null
: new DetachedObject(entity, getSession()));
---
In DetachedObject you do
---
public DetachedObject( Object object, Session session )
{
this.object = object;
reattachRequired = session.getSessionFactory().getClassMetadata( object.getClass() )
.getIdentifier( object, EntityMode.POJO ) != null;
}
---
This means that you save the object in a DetachedObject and this _whole_
DetachedObject in the WebSession afterwards, don't you?
Wouldn't you have to call object.getId() or something like that in
DetachedObject and save this in an private Integer instead?
Kind regards,
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
My code doesn't use Spring as the container either. I use HiveMind. My
code just uses the Spring classes inside HiveMind. Why re-invent the
wheel?
> On 21. Apr 2006 - 14:44:59, James Carman wrote:
> | 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
>
> I don't use Spring but I would like to be able to use your
> PersistenceStrategy. ;)
> Is someone out there who has adapted it (or something similar) to
> Hivemind?
>
> Thanks!
> Andreas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
James Carman, President
Carman Consulting, Inc.
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Andreas Bulling <sp...@phoenix.hadiko.de>.
On 21. Apr 2006 - 14:44:59, James Carman wrote:
| 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
I don't use Spring but I would like to be able to use your PersistenceStrategy. ;)
Is someone out there who has adapted it (or something similar) to Hivemind?
Thanks!
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: tapestry/hibernate sessions & pageBeginRender()
Posted by Hans L <ha...@gmail.com>.
Thanks for the suggestion -- I will try this out!
Hans
James Carman wrote:
> 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:news@sea.gmane.org] 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: tapestry/hibernate sessions & pageBeginRender()
Posted by James Carman <ja...@carmanconsulting.com>.
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:news@sea.gmane.org] 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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org