T5: how to use a detached hibernate object in a form?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

T5: how to use a detached hibernate object in a form?

angelochen
Hi,

I have code like following, but it generate an exception when form submits, i thought there is only one Session open and usr is a detached instance, any hints on how to fix this? thanks.

A.C.

Illegal attempt to associate a collection with two open sessions


@Persist
private Usr usr;


@Inject
private Session _session;

public Class onActivate(Long id) {
        usr = (Usr)_session.get(Usr.class, id);
}

String onSuccess() {
        _session.saveOrUpdate(usr);          
         return null;
}

Reply | Threaded
Open this post in threaded view
|

Re: T5: how to use a detached hibernate object in a form?

lasitha
http://thread.gmane.org/gmane.comp.java.tapestry.user/53095/focus=53170
Cheers, lasitha.

On 10/22/07, Angelo Chen <[hidden email]> wrote:

>
> Hi,
>
> I have code like following, but it generate an exception when form submits,
> i thought there is only one Session open and usr is a detached instance, any
> hints on how to fix this? thanks.
>
> A.C.
>
> Illegal attempt to associate a collection with two open sessions
>
>
> @Persist
> private Usr usr;
>
>
> @Inject
> private Session _session;
>
> public Class onActivate(Long id) {
>         usr = (Usr)_session.get(Usr.class, id);
> }
>
> String onSuccess() {
>         _session.saveOrUpdate(usr);
>          return null;
> }
>
>
> --
> View this message in context: http://www.nabble.com/T5%3A-how-to-use-a-detached-hibernate-object-in-a-form--tf4669657.html#a13339389
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5: how to use a detached hibernate object in a form?

angelochen
Hi Lasitha,

Thanks for the link, but what's the conclusion? Tapestry-hibernate has to be updated to close the session? there seems no decision in that thread. I use now merge which get rid of the problem so far. I'm new to this T5 form handling, if I understand correctly: onActivate the session was opened because a object is retrieved, then  automatically close. onSuccess another session was opened because of call to update/merge, am I right here?
A.C.

lasitha wrote
http://thread.gmane.org/gmane.comp.java.tapestry.user/53095/focus=53170
Cheers, lasitha.

On 10/22/07, Angelo Chen <angelochen960@yahoo.com.hk> wrote:
>
> Hi,
>
> I have code like following, but it generate an exception when form submits,
> i thought there is only one Session open and usr is a detached instance, any
> hints on how to fix this? thanks.
>
> A.C.
>
> Illegal attempt to associate a collection with two open sessions
>
>
> @Persist
> private Usr usr;
>
>
> @Inject
> private Session _session;
>
> public Class onActivate(Long id) {
>         usr = (Usr)_session.get(Usr.class, id);
> }
>
> String onSuccess() {
>         _session.saveOrUpdate(usr);
>          return null;
> }
>
>
> --
> View this message in context: http://www.nabble.com/T5%3A-how-to-use-a-detached-hibernate-object-in-a-form--tf4669657.html#a13339389
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Reply | Threaded
Open this post in threaded view
|

Re: T5: how to use a detached hibernate object in a form?

Thiago H de Paula Figueiredo
In reply to this post by angelochen
On Mon, 22 Oct 2007 06:10:42 -0300, Angelo Chen  
<[hidden email]> wrote:

> String onSuccess() {
> _session.saveOrUpdate(usr);
>          return null;
> }

Just do this:

String onSuccess() {
        usr = _session.merge(usr); // take a look at Session.merge() method to  
learn more. :)
        _session.saveOrUpdate(usr);
         return null;
}

--
Thiago H. de Paula Figueiredo
Desenvolvedor, Instrutor e Consultor de Tecnologia
Eteg Tecnologia da Informação Ltda.
http://www.eteg.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5: how to use a detached hibernate object in a form?

Christian Gruber-5
In reply to this post by angelochen
Yes. Hibernate sessions re cheap, however, though it would be nice to  
keep some of the session validity a teensy bit longer to get more  
caching value out of the L1 cache.  But what you say is true,  
and .merge() is probably what you need here.

Christian.

On 22-Oct-07, at 6:50 AM, Angelo Chen wrote:

>
> Hi Lasitha,
>
> Thanks for the link, but what's the conclusion? Tapestry-hibernate  
> has to be
> updated to close the session? there seems no decision in that  
> thread. I use
> now merge which get rid of the problem so far. I'm new to this T5 form
> handling, if I understand correctly: onActivate the session was opened
> because a object is retrieved, then  automatically close. onSuccess  
> another
> session was opened because of call to update/merge, am I right here?
> A.C.
>
>
> lasitha wrote:
>>
>> http://thread.gmane.org/gmane.comp.java.tapestry.user/53095/focus=53170
>> Cheers, lasitha.
>>
>> On 10/22/07, Angelo Chen <[hidden email]> wrote:
>>>
>>> Hi,
>>>
>>> I have code like following, but it generate an exception when form
>>> submits,
>>> i thought there is only one Session open and usr is a detached  
>>> instance,
>>> any
>>> hints on how to fix this? thanks.
>>>
>>> A.C.
>>>
>>> Illegal attempt to associate a collection with two open sessions
>>>
>>>
>>> @Persist
>>> private Usr usr;
>>>
>>>
>>> @Inject
>>> private Session _session;
>>>
>>> public Class onActivate(Long id) {
>>>        usr = (Usr)_session.get(Usr.class, id);
>>> }
>>>
>>> String onSuccess() {
>>>        _session.saveOrUpdate(usr);
>>>         return null;
>>> }
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/T5%3A-how-to-use-a-detached-hibernate-object-in-a-form--tf4669657.html#a13339389
>>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/T5%3A-how-to-use-a-detached-hibernate-object-in-a-form--tf4669657.html#a13340586
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5: how to use a detached hibernate object in a form?

lasitha
> On 22-Oct-07, at 6:50 AM, Angelo Chen wrote:
> >
> > Thanks for the link, but what's the conclusion? Tapestry-hibernate
> > has to be updated to close the session? there seems no decision in
> > that thread.

That is _my_ conclusion :).  You're right, that thread didn't really
go anywhere, partly because i dropped the ball and didn't go on to
post this as an issue on jira.  I've now done so:
https://issues.apache.org/jira/browse/TAPESTRY-1850

> > ... if I understand correctly: onActivate the session was opened
> > because a object is retrieved, then  automatically close. onSuccess
> > another session was opened because of call to update/merge, am I right
> > here?

No, i don't think so.  Both onActivate() and onSuccess() are called
within the same thread, so will actually use the same Session
instance.  Immediately after this though, you will see a new Session
being opened because tapestry doesn't just render the result in the
same thread - it sends a redirect back to the browser, which will in
turn activate your page again.  So:
1. user navigates to page,
    -> onActivate fires, Session 1 is opened.
2. user submits form,
    -> onActivate, binding, validation, onSuccess all occur in Session 2.
3. onSuccess returns null,
    -> tapestry sends a redirect to the browser,
    -> browser asks for the page,
    -> onActivates fires again, Session 3 is opened.
There are of course other events that will fire, i'm highlighting the
ones from your example.

> > I use now merge which get rid of the problem so far.

This will probably work, but i would consider it a workaround.  The
real problem, IMO, is that a previous session hasn't been closed (by
the framework).  The merge method is designed to be used, as i
understand it, in a much less typical scenario - when the object in
your current session needs to be updated with changes made to a copy
of it in a different session (or out of session).  Ok, thats probably
not clear (!), but the point i'm trying to make is you're having to
use a fairly specialized method in hibernate for just a
run-of-the-mill scenario.

If you're convinced by this, patching HibernateSessionManagerImpl.java
as described in the other thread is trivial to do.  Hopefully the
issue will be fixed and you'll only have to do this temporarily.  Of
course you may just stick with merge() in the meantime - i just wanted
to give you my understanding of the problem.

Cheers,
lasitha.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]