LazyInitializationException in Hibernate deepCopy

279 views
Skip to first unread message

esordini

unread,
Jun 25, 2009, 6:12:56 AM6/25/09
to beanlib
Dear all,
I'm trying to "unproxy" a Hibernate 3 (rather complex) Object with
BeanLib prior to XML serialization with XStream. I'm keeping on
getting a LazyInitializationException on a few fields:

ERROR [main] (LazyInitializationException.java:42) - could not
initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy
- no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize
(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation
(AbstractLazyInitializer.java:140)
at net.sf.beanlib.hibernate.UnEnhancer.unenhanceObject
(UnEnhancer.java:120)
at
net.sf.beanlib.hibernate3.Hibernate3JavaBeanReplicator.replicateBean
(Hibernate3JavaBeanReplicator.java:71)
at
net.sf.beanlib.provider.replicator.ReplicatorTemplate.replicateByBeanReplicatable
(ReplicatorTemplate.java:140)
at net.sf.beanlib.provider.replicator.ReplicatorTemplate.replicate
(ReplicatorTemplate.java:135)
[...]
at net.sf.beanlib.hibernate.HibernateBeanReplicator.deepCopy
(HibernateBeanReplicator.java:181)
at net.sf.beanlib.hibernate.HibernateBeanReplicator.deepCopy
(HibernateBeanReplicator.java:157)
[...]

I set SessionImpl logging to maximum verbosity, and it turned out
that:

1) The session is correctly open and closed during the whole execution
of the program, the first of which is because of a transaction closure
in a constructor at the very beginning. Then a new session is opened
which is not closed until the very end.

2) The call to deepCopy() is made within an active session.

My hunch is that for some reason, the objects causing the error are
kind of "stale" (or, rather, detached) ones, belonging to the former
session, on which an initialization is performed but within another
session. As mentioned above, the "stale" entities are loaded by a
constructor very early in the execution flow.

I've banging my head over this problem for quite a while without
finding a solution. Therefore, any help will be highly valued.

Thanks in advance
Emmanuele

Hanson Char

unread,
Jun 25, 2009, 11:49:36 AM6/25/09
to bea...@googlegroups.com
I agree with your hunch - something to do with the deep copy being performed after the respective session is closed.

Gerson Samaniego

unread,
Jul 31, 2009, 10:35:49 AM7/31/09
to beanlib
If you don't have an active session, you can add a
DetailedPropertyFilter to the transformer BeanReplicator. This filter
can exclude uninitialized Hibernate collections to avoid
LazyInizialitationException, this do in propagate method:

Object fromValue = readPropertyValue(fromBean, readerMethod.getName
());
boolean isPersistentCollection =
(PersistentCollection.class.isAssignableFrom(fromValue.getClass()));

if (isPersistentCollection && !Hibernate.isInitialized(fromValue)){
logger.debug("Excluyendo la colección: " + propertyName);
return false;
}

return true;

On Jun 25, 5:12 am, esordini <emmanuele.sord...@gmail.com> wrote:
> Dear all,
> I'm trying to "unproxy" a Hibernate 3 (rather complex) Object with
> BeanLib prior to XML serialization with XStream. I'm keeping on
> getting a LazyInitializationException on a few fields:
>
> ERROR [main] (LazyInitializationException.java:42) - could not
> initialize proxy - no Session
> org.hibernate.LazyInitializationException: could not initialize proxy
> - no Session
>         at org.hibernate.proxy.AbstractLazyInitializer.initialize
> (AbstractLazyInitializer.java:86)
>         at org.hibernate.proxy.AbstractLazyInitializer.getImplementation
> (AbstractLazyInitializer.java:140)
>         at net.sf.beanlib.hibernate.UnEnhancer.unenhanceObject
> (UnEnhancer.java:120)
>         at
> net.sf.beanlib.hibernate3.Hibernate3JavaBeanReplicator.replicateBean
> (Hibernate3JavaBeanReplicator.java:71)
>         at
> net.sf.beanlib.provider.replicator.ReplicatorTemplate.replicateByBeanReplic­atable
Reply all
Reply to author
Forward
0 new messages