-->
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.  [ 9 posts ] 
Author Message
 Post subject: Struts + Spring + Hibernate : LazyInitializationException
PostPosted: Tue Sep 13, 2005 9:29 am 
Newbie

Joined: Tue Sep 13, 2005 9:10 am
Posts: 5
Hibernate version:3.0.5
Spring version:1.2.4
Struts version:1.1

Name and version of the database:PostGreSQL 8.0.3

Bonjour,

J'essaye de mettre en place une application web Struts avec le framework Spring + Hibernate.
Dans un premier temps j'aimerai tester ma connexion à la Base de Données et le chargement d'un objet Hibernate à partir d'une table toute simple - en l'occurence la table s'appelle "commande".

Ma base de données est PostGreSQL et ne contient qu'une table "Commande" qui contient 3 colonnes.

Pour créer mon appli, je suis parti de l'exemple fourni dans la doc Spring avec l'implémentation de la gestion déclarative des transactions avec Spring DAO TransactionInterceptor.

L'exécution de la méthode suivante me renvoie systématquement une exception LazyInitializationException et je ne comprends pas pourquoi ...
public Commande trouveCommandeParID(final int id) {

logger.debug("DEBUT");
Commande order = (Commande) getHibernateTemplate().load(Commande.class, new Integer(id));
logger.debug(order.getNomUtilisateur());
logger.debug("FIN");
return order;
}

J'ai beau regardé dans la doc Spring et dans la doc Hibernate, je ne vois rien que j'ai mal fait. Mais pourtant ... Je ne sais pas ou chercher pour trouver la cause de mon probleme. Quelqu'un pourrait-il me donner une piste ?

Merci

Full stack trace of any exception that occurs:
13 15:06:23,936 ERROR () (org.hibernate.LazyInitializationException.<init>)(19) - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.cgey.cm.om.sample.Commande$$EnhancerByCGLIB$$2d38fbd1.getNomUtilisateur(<generated>)
at com.cgey.cm.DAO.sample.CommandeDAO.trouveCommandeParID(CommandeDAO.java:51)
at com.cgey.cm.service.sample.CommandeService.trouveCommandeParId(CommandeService.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:163)
at $Proxy1.trouveCommandeParId(Unknown Source)
at com.cgey.cm.webapp.actions.login.LoginAction.execute(LoginAction.java:56)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:05 am 
Newbie

Joined: Mon Sep 12, 2005 11:05 am
Posts: 7
Location: Swizerland
hello

est ce qu'il n'y pas des erreur avant ???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:12 am 
Newbie

Joined: Tue Sep 13, 2005 9:10 am
Posts: 5
Malheureusement je n'ai pas d'erreurs avant.
Lorsque je démarre mon application tout se passe bien.
Ensuite lorsque j'exécute la méthode getHibernateTemplate().load(Commande.class, new Integer(id)), j'ai l'impression que ca fonctionne car je crois qu'il m'instancie un objet de type Commande d'apres les traces. Par contre, lorsque j'accede à une propriété de l'objet via order.getNomUtilisateur() par exemple c'est la que l'Exception est soulevée.

Voici un extrait des traces précédentes :
13 16:08:55,211 DEBUG () (com.cgey.cm.webapp.actions.login.LoginAction.execute)(47) - DEBUT
13 16:08:55,211 DEBUG () (com.cgey.cm.webapp.actions.login.LoginAction.execute)(52) - Connexion de l'utilisateur
13 16:08:55,211 DEBUG () (com.cgey.cm.DAO.sample.CommandeDAO.trouveCommandeParID)(39) - DEBUT
13 16:08:55,211 DEBUG () (org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession)(327) - Opening Hibernate Session
13 16:08:55,227 DEBUG () (org.hibernate.impl.SessionImpl.<init>)(250) - opened session at timestamp: 4614637712289792
13 16:08:55,227 DEBUG () (org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad)(143) - loading entity: [com.cgey.cm.om.sample.Commande#1]
13 16:08:55,227 DEBUG () (org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary)(216) - creating new proxy for entity
13 16:08:55,242 DEBUG () (org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery)(322) - after autocommit
13 16:08:55,242 DEBUG () (org.hibernate.impl.SessionImpl.afterTransactionCompletion)(403) - after transaction completion
13 16:08:55,242 DEBUG () (org.springframework.orm.hibernate3.HibernateTemplate.flushIfNecessary)(393) - Eagerly flushing Hibernate session
13 16:08:55,242 DEBUG () (org.springframework.orm.hibernate3.SessionFactoryUtils.doClose)(773) - Closing Hibernate Session
13 16:08:55,242 DEBUG () (org.hibernate.impl.SessionImpl.close)(269) - closing session
13 16:08:55,258 DEBUG () 13 16:08:55,258 ERROR () (org.hibernate.LazyInitializationException.<init>)(19) - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:16 am 
Newbie

Joined: Mon Sep 12, 2005 11:05 am
Posts: 7
Location: Swizerland
dans ton fichier de mapping hibernate tu as esseyé avec le strict minimum ( sans les types, ...) ???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:28 am 
Newbie

Joined: Tue Sep 13, 2005 9:10 am
Posts: 5
Voici mon fichier de config Commande.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 1.1//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.cgey.cm.om.sample.Commande" table="commande">
<id name="id" column="id_commande" type="int">
<generator class="increment"/>
</id>
<property name="nomUtilisateur" type="string"/>
<property name="total" type="int"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:37 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Bonjour,

C'est une erreur "classique" - sur laquelle il y aurait beaucoup à dire...

La solution la plus simple est d'adapter le mapping en:
Code:
<class name="com.cgey.cm.om.sample.Commande" table="commande" lazy="false">

et d'utiliser get au lieu de load pour chercher la Commande.

Le problème de base est que Spring - si l'on ne configure pas davantage - ouvre et ferme une Session à chaque appel de getHibernateTemplate() et que d'autre part load renvoie un Proxy, qui n'est valide que tant que l'on reste dans la même Session Hibernate.

J'espère que cela aide pour les prochains pas...

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:38 am 
Newbie

Joined: Tue Sep 13, 2005 9:10 am
Posts: 5
Suite à ta première remarque, j'ai constaté que dans les logs, lors du démarrage de mon application, j'ai une trace de niveau WARNING

13 16:30:58,158 DEBUG () (net.sf.ehcache.CacheManager.create)(191) - Creating new CacheManager with default config
13 16:30:58,190 DEBUG () (net.sf.ehcache.CacheManager.configure)(164) - Configuring ehcache from classpath.
13 16:30:58,236 WARN () (net.sf.ehcache.config.Configurator.configure)(126) - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/workspace/cm-webapp/target/cm-webapp/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
13 16:30:58,268 DEBUG () (net.sf.ehcache.config.Configuration$DiskStore.setPath)(185) - Disk Store Path: C:\Program Files\Apache Group\Tomcat 4.1\temp

Je ne sais pas si ca influe sur mon probleme.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:40 am 
Newbie

Joined: Mon Sep 12, 2005 11:05 am
Posts: 7
Location: Swizerland
bien vue ErickFK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:48 am 
Newbie

Joined: Tue Sep 13, 2005 9:10 am
Posts: 5
Merci ErikFK ca résout mon problème :-)

Merci kmakma pour ton aide


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.