IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[Hibernate3] probleme de LazyInitializationException


Sujet :

Hibernate Java

  1. #1
    Rédacteur

    Homme Profil pro
    Geek entrepreneur
    Inscrit en
    Novembre 2004
    Messages
    1 224
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Geek entrepreneur

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 224
    Par défaut [Hibernate3] probleme de LazyInitializationException
    LazyInitializationException

    Salut,
    Je me heurte a un probleme incompréhensible de LazyInitializationException.
    Dans un service, j'utilise des DAOs. Ce service fait en principe partie d'une transaction (définie avec Spring) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
     
    	<bean id="transactionInterceptor"
    		class="org.springframework.transaction.interceptor.TransactionInterceptor">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
     
    	<bean id="autoProxyCreator"
    		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    		<property name="interceptorNames">
    			<list>
    				<idref local="transactionInterceptor" />
    			</list>
    		</property>
    		<property name="beanNames">
    			<list>
    				<idref local="MyService" />
    			</list>
    		</property>
    	</bean>
    En principe, je suis donc bien dans une transaction pour chaque méthode de MyService.
    Dans ce service, je fais une opération de ce type :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Set<Childs> childs = father.getChilds();
    Childs firstChild = (Childs)child.toArray()[0];
    Le father a déjà été initialisé plus tot et manipulé dans un objet métier. Je suis repassé par mon service pour faire l'appel a getChilds afin d'éviter un probleme de LazyInstantiation car mon association est en lazy loading.

    En tests unitaires avec Spring et derby, ca fonctionne.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        public void testGetChildByIndexForFather ()
        {
            // chaque manipulation bdd est faite dans le service qui doit participer a un transaction
            Father father = __myService.getFatherById(1);
            // Le load est fait dans le service pour éviter la LazyInitializationException
            Childs firstChild = __myService.getChildByIndexForFather(father,0);
            assertNotNull(firstChild );
        }
    Par contre dans un cas réel (avec les vrais noms des classes) :

    SEVERE failed to lazily initialize a collection of role: net.sf.l2j.gameserver.model.forum.Topic.postses, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net.sf.l2j.gameserver.model.forum.Topic.postses, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    at org.hibernate.collection.PersistentSet.toArray(PersistentSet.java:171)
    at net.sf.l2j.gameserver.services.forum.ForumService.getPostByIndexForTopic(ForumService.java:291)
    at net.sf.l2j.gameserver.services.forum.ForumService$$FastClassByCGLIB$$527cee2c.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:675)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:616)
    at net.sf.l2j.gameserver.services.forum.ForumService$$EnhancerByCGLIB$$81b0fdf5.getPostByIndexForTopic(<generated>)
    at net.sf.l2j.gameserver.communitybbs.Manager.PostBBSManager.ShowMemoPost(PostBBSManager.java:220)
    at net.sf.l2j.gameserver.communitybbs.Manager.PostBBSManager.showPost(PostBBSManager.java:149)
    at net.sf.l2j.gameserver.communitybbs.Manager.PostBBSManager.parsecmd(PostBBSManager.java:89)
    at net.sf.l2j.gameserver.communitybbs.CommunityBoard.handleCommands(CommunityBoard.java:71)
    at net.sf.l2j.gameserver.clientpackets.RequestBypassToServer.runImpl(RequestBypassToServer.java:220)
    at net.sf.l2j.gameserver.clientpackets.ClientBasePacket.run(ClientBasePacket.java:81)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Pourtant dans ForumService je dois avoir ouvert une transaction et ma session ne doit pas être close.
    Qu'est ce que j'ai loupé ?

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Par défaut
    As-tu réattaché ton objet à une session hibernate ?

  3. #3
    Rédacteur

    Homme Profil pro
    Geek entrepreneur
    Inscrit en
    Novembre 2004
    Messages
    1 224
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Geek entrepreneur

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 224
    Par défaut
    Non. Et ca semble venir de la d'après des collègues. Je dois revenir jusqu'au DAO et réattacher a une session pour récupérer les fils.
    Par contre personne ne comprend pourquoi le test unitaire marche ^^

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Probleme de rafraichissement d'un BDGrid
    Par marmotte dans le forum Bases de données
    Réponses: 10
    Dernier message: 28/05/2004, 18h07
  2. Réponses: 2
    Dernier message: 30/05/2002, 08h54
  3. Probleme sur les chaines de caractere
    Par scorpiwolf dans le forum C
    Réponses: 8
    Dernier message: 06/05/2002, 19h01
  4. [Kylix] Probleme d'execution de programmes...
    Par yopziggy dans le forum EDI
    Réponses: 19
    Dernier message: 03/05/2002, 14h50
  5. [Kylix] Probleme de nombre flottant!!
    Par yopziggy dans le forum EDI
    Réponses: 5
    Dernier message: 02/05/2002, 10h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo