Still get LazyInitializationException with OpenSessionInView
Hi there,
I'm using OpenSessionInViewInterceptor, but I still get LazyInitializationException.
I am trying to build a pet project with Hibernate + Spring + SWF + JSF (Rich Faces) dealing with Unit of Measure (UoM). I store UoM standards in a DB table,
e.g. metric system, imperial system, american system, etc. for each system, I store their localized names in DB as well, e.g. for metric system (id = 1), I
also store in DB in a separate table what it's called in english, french, japanese, chinese. My application will need to display these localized name for
each standard system for a given locale. I'm having getting LazyInitializationException when i try to get these names even with OpenSessionInViewInterceptor
Here are my setting in spring:Code:
lt;bean id=quot;requestToHanlderMappingquot;
class=quot;org..web.servlet.handler.SimpleuclHandlerMappingquot;gt;
lt;property name=quot;mappingsquot;gt;
lt;valuegt;
lt;!-- Spring JSF integration --gt;
/flows/*=flowController
/flows/*/*=flowController
lt;/valuegt;
lt;/propertygt;
lt;property name=quot;alwaysUseFullPathquot; value=quot;truequot;/gt;
lt;property name=quot;interceptorsquot;gt;
lt;listgt;
lt;ref bean=quot;openSessionInViewInterceptorquot; /gt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;lt;bean id=quot;openSessionInViewInterceptorquot;
class=quot;org..orm.hibernate3.support.OpenSessionInViewInterceptorquot;gt;
lt;property name=quot;sessionFactoryquot;gt;
lt;ref bean=quot;mySessionFactoryquot; /gt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;mySessionFactoryquot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot;gt;
lt;property name=quot;dataSourcequot; ref=quot;myDataSourcequot;/gt;
lt;property name=quot;mappingResourcesquot;gt;
lt;listgt;
lt;valuegt;net/rum/domain/UoMStandardSystem.hbm.xmllt;/valuegt;
lt;/listgt;
lt;/propertygt;
lt;property name=quot;hibernatePropertiesquot;gt;
lt;valuegt;
hibernate.dialect=org.hibernate.dialect.MySQLDialect
lt;/valuegt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;repoquot; class=quot;net.rum.repository.hibernate.UoMStandardSystemRepositoryHibernateDAOquot;gt;
lt;property name=quot;sessionFactoryquot; ref=quot;mySessionFactoryquot;/gt;
lt;/beangt;
lt;bean id=quot;txManagerquot;
class=quot;org..jdbc.datasource.DataSourceTransactionManagerquot;gt;
lt;property name=quot;dataSourcequot; ref=quot;myDataSourcequot; /gt;
lt;/beangt;
lt;!-- the transactional advice (what 'happens'; see the lt;aop:advisor/gt; bean below) --gt;
lt;tx:advice id=quot;txAdvicequot; transaction-manager=quot;txManagerquot;gt;
lt;!-- the transactional semantics... --gt;
lt;tx:attributesgt;
lt;!-- all methods starting with 'get' are read-only --gt;
lt;tx:method name=quot;get*quot; read-only=quot;truequot; /gt;
lt;!-- other methods use the default transaction settings (see below) --gt;
lt;tx:method name=quot;*quot; /gt;
lt;/tx:attributesgt;
lt;/tx:advicegt;
lt;aop:configgt;
lt;aop:pointcut id=quot;repoOpsquot;
expression=quot;execution(* net.rum.repository.hibernate.UoMStandardSystemRepositoryHibernateDAO.*(..))quot; /gt;
lt;aop:advisor advice-ref=quot;txAdvicequot; pointcut-ref=quot;repoOpsquot; /gt;
lt;/aop:configgt;
lt;bean id=quot;repoLocalizedquot; class=quot;net.rum.pres.UoMStandardSystemRepositoryImplLocalizedquot;gt; lt;property name=quot;uoMStandardSystemRepositoryquot; ref=quot;repoquot;/gt;
lt;/beangt;
In SWF, I invoke Code:
lt;on-startgt;
lt;evaluate expression=quot;repoLocalized.findAll(Locale.ENGLISH)quot; result=quot;flowScope.uoMStandardsquot; result-type=quot;dataModelquot; /gt;
lt;/on-startgt;
In JSF, I invokeCode:
lt;h:formgt;
lt;rich:dataTable id=quot;uomStandardsquot; value=quot;#{uoMStandards}quot; var=quot;uoMStandardquot;gt;
lt;f:facet name=quot;headerquot;gt;UoM Standardslt;/f:facetgt;
lt;h:columngt;
lt;f:facet name=quot;headerquot;gt;Idlt;/f:facetgt;
#{uoMStandard.uom.uomStandardSystemId}
lt;/h:columngt;
lt;h:columngt;
lt;f:facet name=quot;headerquot;gt;Namelt;/f:facetgt;
#{uoMStandard.localizedName}
lt;/h:columngt;
lt;/rich:dataTablegt;
lt;/h:formgt;
whereCode:
public class UoMStandardSystemRepositoryHibernateDAO extends
HibernateDaoSupport implements UoMStandardSystemRepository {
...
@Override
public UoMStandardSystem findById(int id) {
Listlt;UoMStandardSystemgt; results = this.getHibernateTemplate().find(
quot;from net.rum.domain.UoMStandardSystem as uom where uom.uomStandardSystemId = ?quot;, Integer.valueOf(id));
if (results == null || results.size() == 0) {
return null; // should throw ex
} return results.get(0);
}
}public class UoMStandardSystemRepositoryImplLocalized implements
UoMStandardSystemRepositoryLocalized {
UoMStandardSystemRepository uoMStandardSystemRepository;
...
@Override
public Listlt;UoMStandardSystemLocalizedgt; findAll(Locale l) {
Listlt;UoMStandardSystemgt; uoms = uoMStandardSystemRepository.findAll();
Listlt;UoMStandardSystemLocalizedgt; luoms = new ArrayListlt;UoMStandardSystemLocalizedgt;(uoms.size());
for (UoMStandardSystem uom : uoms) {
UoMStandardSystemLocalized luom = new UoMStandardSystemLocalized(uom, l);
luoms.add(luom);
}
return luoms;
}
}public class UoMStandardSystemLocalized implements Serializable {
private UoMStandardSystem uom;
private Locale locale;
...
public String getLocalizedName() {
return uom.getName(locale);
}
}
So can anyone enlighten me what did I do wrong?thx very much in advance and have a merry christmas and happy new year!
chuck
I found out from SWF forum that OpenSessionInView will not help SWF case which has several redirects. So I need to use SWF's lt;persistence-context/gt; instead. see this thread:
showthread.php?t=58645
and SWF Ref Manual chap 6. Flow Managed Persistence
cheers
chuck |