On Nov 22, 2007 6:27 PM, Yunhua Sang <[EMAIL PROTECTED]> wrote:
> ...
> Steps:
> 1. edit a people' firstname in Page1, It's possible I don't need to
> show address here, so get Addresses() is NOT called,
> 2. I submit the form,  modify firstname, save people.
> 3. I render people (already be detached) in page1 again  WITH address
> info, i.e. call getAddresses(); here LazyInitializationException
> happens ...
>
> I knew merge()  or refresh() both work, but where should I call them?
> in beginRender()? sounds not good.

Hello Richard,

Where are you loading People?  That same place may a good place to start.

onActivate(), pageAttached() or onPrepare() seem like good candidates;
it sort of depends on the composition of your page and the complexity
of events it encounters.  The first is often not ideal because it can
be called numerous times within a single request (when contained
component events bubble up).

Since the above events fire on both the original request and the one
from the redirect, you'll probably need a null check to differentiate:
load from db if null, attach to current session if not.

Also, consider using Session.lock(with LockMode.NONE)[1] instead of
merge() or refresh().  I don't know when it became fashionable to use
merge(), but it essentially gives you a whole new instance and you
have to be careful to discard the old one[2].  refresh() has the
benefit of rereading state from the db, but if you think the
underlying data might change between the save and redirect you should
probably lock with LockMode.READ and handle the version check failure.

Lastly, i must admit that i usually just reload the object from the db
instead of @Persist-ing it.  It's just less hassle and has rarely been
too slow.

Oh, and if you do stay with @Persist, consider using flash persistence
instead of the default.

Cheers, lasitha.

Notes:
[1] 
http://www.hibernate.org/hib_docs/core/api/org/hibernate/Session.html#lock(java.lang.Object,%20org.hibernate.LockMode)
[2] merge() does have the advantage of allowing you to call it after
modifying the entity, but if you can attach the entities early in the
request you're more likely to get the most out of hibernate's dirty
checking mechanism.

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

Reply via email to