-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: URGENT - SPRING 2.5 + HIBERNATE 3.0 - PLEASE HELP ME!!!!!
PostPosted: Thu Mar 25, 2010 6:16 am 
Newbie

Joined: Thu Mar 25, 2010 6:15 am
Posts: 2
Good morning,

I'm developing a web-app that manages the E-Procurement.
I need to understand how to work Spring with Hibernate.
In particular I need to understand the lazing concepts!
The Db structure is complex because I've more one-to-many and many-to-many relationship.
I would to fetch child lists ONLY when I the getChildsList() method is called but at this time I'm receiving the Lazy Loading Exception in presentation layer.
My config:

**********
web.xml
**********
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-service-context.xml</param-value>
</context-param>
<filter>
<filter-name>OSIVFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OSIVFilter</filter-name>
<url-pattern>*.faces</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
</listener>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

********************
spring-service-context.xml
********************
<beans .......>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
<property name="locations">
<value>file:${RFX_HOME}/RFX.properties</value>
</property>
</bean>
<bean class="it.niuma.rfx.beanServices.manageXml.ManageX ml">
<property name="beanProperties">
<ref bean="beanProperties"/>
</property>
</bean>
<bean class="it.niuma.rfx.beanServices.managePdf.ManageP df">
<property name="beanProperties">
<ref bean="beanProperties"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
<property name="jndiName">
<value>java:/RFX_JNDI</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="lobHandler">
<ref bean="lobHandler" />
</property>
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="mappingResources">
<list>
<value>/it/niuma/rfx/hbm-mappings/Rfx.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager"/>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aopointcut id="businessOperationMethods" expression="execution(* it.niuma.rfx.services.*.*.*(..))" />
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="businessOperationMethods" />
</aop:config>
<bean id="rfxMgmtServiceImpl" class="it.niuma.rfx.services.rfxMgmtService.RfxMgm tServiceImpl">
<property name="rfxDAO">
<ref bean="rfxDAO"/>
</property>
</bean>
<bean id="rfxDAO" class="it.niuma.rfx.dao.rfx.RfxDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>

*******************
rfx.hbm.xml example
*******************
<!-- PROPERTIES -->
<many-to-one name="tipoRfx" class="it.niuma.rfx.model.TipoRfx" fetch="join" lazy="false">
<column name="TIPO_RFX" precision="22" scale="0" />
</many-to-one>
<!-- OTHER MANY-TO-ONE AND PROPERTIES -->
<set name="categorieMerceologiche" table="RFX_CAT_MERC" cascade="all" batch-size="10" order-by="ID_CAT_MERC asc" sort="unsorted">
<key>
<column name="ID_RFX" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="it.niuma.rfx.model.CatMerc">
<column name="ID_CAT_MERC" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
<set name="fornitori" table="RFX_FORNITORI" cascade="all" batch-size="100" >
<key>
<column name="ID_RFX" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="it.niuma.rfx.model.Fornitore">
<column name="ID_FORNITORE" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
<!-- OTHERS MANY-TO-MANY -->

********************
rfxDAOImpl
********************
public Collection<Rfx> findRfxByStatus(List<Status> statiRfx) throws RFXDatabaseException {

List<Integer> results = null;
List<Rfx> rfxResults = null;
try{
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Rfx.class);
detachedCriteria.add(Restrictions.in("statoRfx", statiRfx));
detachedCriteria.setProjection(Projections.distinc t(Projections.property("idRfx")));
results = getHibernateTemplate().findByCriteria(detachedCrit eria);

if(results != null && results.size() > 0){
DetachedCriteria detachedCriteria2 = DetachedCriteria.forClass(Rfx.class);
detachedCriteria2.add(Restrictions.in("idRfx", results));
detachedCriteria2.addOrder(Order.desc("dataCreazio ne"));
rfxResults = getHibernateTemplate().findByCriteria(detachedCrit eria2);
// Initialize all collections
if (rfxResults != null && !rfxResults.isEmpty()) {
for (Rfx rfx: rfxResults) {
getHibernateTemplate().initialize(rfx.getTemplates ());
......
if (rfx.getRfxRiferimento() != null && !rfx.getRfxRiferimento().isEmpty()) {
for (RfxRiferimento rfxRif: rfx.getRfxRiferimento()) {
if(rfxRif.getRisposte() != null && !rfxRif.getRisposte().isEmpty())
getHibernateTemplate().initialize(rfxRif.getRispos te());
}
}
getHibernateTemplate().initialize(rfx.getRfxCompil ate());
if (rfx.getRfxCompilate() != null && !rfx.getRfxCompilate().isEmpty()) {
for (RfxCompilata rfxComp: rfx.getRfxCompilate()) {
if(rfxComp.getCompilataDa() != null)
getHibernateTemplate().initialize(rfxComp.getCompi lataDa().getFornitore().getReferentiAzienda());
.....
}
}
}
}
getHibernateTemplate().initialize(rfx.getCategorie Merceologiche());
}
}
}
}
catch(Exception exc){
exc.printStackTrace();
throw new RFXDatabaseException(RFXDatabaseErrors.SELECT_EXC) ;
}
return rfxResults;
}

If I don't initialize the childs list I receive the lazy exception.
Sorry for my poor english
Thanks in advance fro any suggestion!
Cheers, Valentina


Top
 Profile  
 
 Post subject: Re: URGENT - SPRING 2.5 + HIBERNATE 3.0 - PLEASE HELP ME!!!!!
PostPosted: Fri Mar 26, 2010 10:10 am 
Newbie

Joined: Mon Mar 01, 2010 7:17 am
Posts: 10
Hi,

I'll start by saying that I'm not an expert on this subject. Nonetheless, I believe that what's causing the exception is that you're calling the get method on a transaction that ends before the retrieved entitity object reaches the presentation layer. Therefore, if that object does not have all its data, I believe it is impossible for Hibernate to go back and get the remaining data, since the original transaction has already ended.

If you really need to pull that information out of the database, call the getXXX method that retrieves the entity's children objects just before ending the transaction, that is, returning from the method that is marked as transactional.

Kind regards,
Nuno Guerreiro


Top
 Profile  
 
 Post subject: Re: URGENT - SPRING 2.5 + HIBERNATE 3.0 - PLEASE HELP ME!!!!!
PostPosted: Sun Mar 28, 2010 8:47 am 
Newbie

Joined: Sun Mar 28, 2010 8:41 am
Posts: 4
Hi,

Dear your session is getting over before you make a call to retrieve child list.
your session gets over with transaction so make sure you make a call to getChildList before your
transaction gets over.

Regards
Sidharth Sharma
PS : Make sure your transaction is working fine.


Top
 Profile  
 
 Post subject: Re: URGENT - SPRING 2.5 + HIBERNATE 3.0 - PLEASE HELP ME!!!!!
PostPosted: Mon Mar 29, 2010 4:24 am 
Newbie

Joined: Thu Mar 25, 2010 6:15 am
Posts: 2
Hi!

Thanks a lot for the replies guys!!!!
I'm sure my transaction is working fine!

I have to call the getChildList() method before ending the transaction.......
Infact I'm using the getHibernateTemplate().initialize(...) method to retrieve the childs objects.
I thought that this practice was wrong......

Have a nice day!

Cheers, Valentina


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.