-->
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: Spring Hibernate LazyInitializaionException
PostPosted: Tue Jan 10, 2006 8:31 am 
Newbie

Joined: Thu Dec 15, 2005 6:02 am
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Bonjour,

La situation:

Je travaille avec Hibernate, Spring et Struts sur Tomcat 5.0.

J'ai une relation many-to-many entre Employee et Function (via une table de lien).

J'ai donc employee.hbm.xml :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="be.arista.HRM.model" >
   <class name="Employee" table="t_employee" dynamic-update="true" dynamic-insert="true">
      <id name="id" column="employee_id" type="java.lang.Integer">
         <generator class="native" />
      </id>

      <property name="name" column="ee_name" type="java.lang.String" update="true" insert="true" not-null="true" />

      <set name="functions" table="t_emplfunctionhrm" lazy="true" cascade="save-update">
         <key column="ef_employee"/>
         <many-to-many class="Function" column="ef_function"/>
      </set>
   </class>
</hibernate-mapping>


lorsuqe je suis dans ma servlet (Action) j'appelle
Code:
employeeService.getEmployee(id)
et cela fonctionne très bien. Mais lorsque sur la ligne suivante je veux appelé employee.getFunctions() il me retourne une erreur : LazyInitializationException

J'ai donc suivi cet article : http://www.jroller.com/page/kbaum?entry ... n_with_dao

Mais sans succès ... :(

Pour comprendre le code qui suit : j'ai dans mon Action un objet business EmployeeService qui a lui-même un object EmployeeDAO. J'appelle donc employeeService.getEmployee() qui vient du code :
Code:
   public Employee getEmployee(Integer id) throws DAOException {
   return getHibernateTemplate().get(Employee.class, id);
   }


Ensuite j'appelle employee.getFunctions() Getter qui me retourne un Set.

Voici les parties de codes que j'ai :
Code:
   ...   
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   ...
   </bean>
   <!--  Web Interceptors -->
   <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="interceptors">
         <list>
            <ref bean="openSessionInViewInterceptor" />
         </list>
      </property>
   </bean>

   <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>

   <!-- TransactionManager -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>

   <!-- DAO Objects -->
   <bean id="employeeDAO" class="be.arista.HRM.dao.hibernateImpl.EmployeeDAOHibernate">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>
   <bean id="functionDAO" class="be.arista.HRM.dao.hibernateImpl.FunctionDAOHibernate">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>
   <!-- Transaction template for Services -->
   <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
         <ref bean="transactionManager" />
      </property>
      <property name="transactionAttributes">
         <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="set*">PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="*">PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,readOnly</prop>
         </props>
      </property>
   </bean>

   <!-- Services -->
   <bean id="employeeService" parent="txProxyTemplate">
      <property name="target">
         <bean class="be.arista.HRM.service.springImpl.EmployeeServiceSpring">
            <property name="employeeDAO" ref="employeeDAO" />
         </bean>
      </property>
   </bean>
   ...



Merci déjà pour l'aide que vous pourrez m'apporter ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:16 am 
Beginner
Beginner

Joined: Tue Sep 23, 2003 5:00 pm
Posts: 40
Salut,

Tu devrais revoir ce point de la doc. Dans le mapping de functions, tu as mis lazy="true", lce qui veut que tes functions ne seront chargées que lorsque tu feras un employee.getFunctions().

Deux solutions à ton problème :

- tu mets lazt="false" (la config par défaut) mais dans ce cas les functions seront chargées en même temps que employee

- tu t'assures de faire des employee.getFunctions() à l'intérieur d'un Session hibernate

sylvain_2020


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:29 am 
Newbie

Joined: Thu Dec 15, 2005 6:02 am
Posts: 8
sylvain_2020 wrote:
Salut,

Tu devrais revoir ce point de la doc. Dans le mapping de functions, tu as mis lazy="true", lce qui veut que tes functions ne seront chargées que lorsque tu feras un employee.getFunctions().

Deux solutions à ton problème :

- tu mets lazt="false" (la config par défaut) mais dans ce cas les functions seront chargées en même temps que employee

- tu t'assures de faire des employee.getFunctions() à l'intérieur d'un Session hibernate

sylvain_2020


Merci Sylvain

Je ne veux pas la première solution car je n'aurai pas que des fonctions liées aux employés et donc on devine très bien l'état de la mémoire lorsque je vais demander la liste des employés.

Pour la deuxième solution c'est pour cela qu'il existe l'objet OpenSessionInView dans Spring mais je n'arrive pas à le faire fonctionner.

Une idée ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:55 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 5:00 pm
Posts: 40
Je ne peux pas trop d'aider, je ne l'ai jamais utilisé ...


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.