[jboss-user] [EJB 3.0] - Re: check if a lazy property is loaded
vboerchers
do-not-reply at jboss.com
Tue Oct 24 18:38:58 EDT 2006
Thanks for the answer ablevine! It works exactly as you wrote. I've implemented some helper methods that does everything we need:
| public static String getObjectDescription(Object o)
| {
| if (o instanceof HibernateProxy)
| {
| LazyInitializer initializer = ((HibernateProxy) o)
| .getHibernateLazyInitializer();
| return initializer.getEntityName()
| + "#" + initializer.getIdentifier();
| }
| return o.toString();
| }
|
This one is also nice:
| public static boolean canBeUsed(Object o)
| {
| if (o instanceof HibernateProxy)
| {
| LazyInitializer initializer = ((HibernateProxy) o)
| .getHibernateLazyInitializer();
| // if already initialized - use it!
| if ( ! initializer.isUninitialized())
| return true;
| // if the session still works - use it!
| return initializer.getSession() != null
| && initializer.getSession().isOpen();
| }
| return true;
| }
|
The ultimative cure for LazyInitializationExceptions is a combination of canBeUsed() and a dedicated service that does something like that:
| mEm.find(Class.forName(initializer.getEntityName())
| , initializer.getIdentifier());
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3980557#3980557
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3980557
More information about the jboss-user
mailing list