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:
In SWF, I invokeCode:<bean id="requestToHanlderMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> <!-- Spring JSF integration --> /flows/*=flowController /flows/*/*=flowController </value> </property> <property name="alwaysUseFullPath" value="true"/> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor" /> </list> </property> </bean> <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"> <ref bean="mySessionFactory" /> </property> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>net/rum/domain/UoMStandardSystem.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect </value> </property> </bean> <bean id="repo" class="net.rum.repository.hibernate.UoMStandardSystemRepositoryHibernateDAO"> <property name="sessionFactory" ref="mySessionFactory"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myDataSource" /> </bean> <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- all methods starting with 'get' are read-only --> <tx:method name="get*" read-only="true" /> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="repoOps" expression="execution(* net.rum.repository.hibernate.UoMStandardSystemRepositoryHibernateDAO.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="repoOps" /> </aop:config> <bean id="repoLocalized" class="net.rum.pres.UoMStandardSystemRepositoryImplLocalized"> <property name="uoMStandardSystemRepository" ref="repo"/> </bean>
In JSF, I invokeCode:<on-start> <evaluate expression="repoLocalized.findAll(Locale.ENGLISH)" result="flowScope.uoMStandards" result-type="dataModel" /> </on-start>
whereCode:<h:form> <rich:dataTable id="uomStandards" value="#{uoMStandards}" var="uoMStandard"> <f:facet name="header">UoM Standards</f:facet> <h:column> <f:facet name="header">Id</f:facet> #{uoMStandard.uom.uomStandardSystemId} </h:column> <h:column> <f:facet name="header">Name</f:facet> #{uoMStandard.localizedName} </h:column> </rich:dataTable> </h:form>
Code:public class UoMStandardSystemRepositoryHibernateDAO extends HibernateDaoSupport implements UoMStandardSystemRepository { ... @Override public UoMStandardSystem findById(int id) { List<UoMStandardSystem> results = this.getHibernateTemplate().find( "from net.rum.domain.UoMStandardSystem as uom where uom.uomStandardSystemId = ?", 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 List<UoMStandardSystemLocalized> findAll(Locale l) { List<UoMStandardSystem> uoms = uoMStandardSystemRepository.findAll(); List<UoMStandardSystemLocalized> luoms = new ArrayList<UoMStandardSystemLocalized>(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


Reply With Quote