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_daoMais 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 ...