Bonjour,
J'ai le problème suivant, j'ai une relation 1-n.
Entre ma classe Entreprise et ma classe Employe configurée comme ci-dessous.
Lorsque j'essaye d'accéder à la liste d'employés à partir de mon entreprise j'ai l'exception ci-dessous.
Pourtant, j'ai bien configuré mon web.xml pour utiliser le pattern Session-In-View.

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
30
31
32
33
34
35
36
07/10/2009 17:04:48,523 20008 [http-8084-2] ERROR org.hibernate.LazyInitializationException  - failed to lazily initialize a collection of role: fr.test.dao.Entreprise.listeEmployes, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: fr.test.dao.Entreprise.listeEmployes, 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.PersistentBag.iterator(PersistentBag.java:249)
        at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:82)
        at org.apache.struts2.components.IteratorComponent.start(IteratorComponent.java:230)
        at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)
        at org.apache.jsp.WEB_002dINF.jsp.admin.entreprise.tabEmployes_jsp._jspx_meth_s_005fiterator_005f0(tabColonnes_jsp.java:708)
        at org.apache.jsp.WEB_002dINF.jsp.admin.entreprise.tabEmployes_jsp._jspx_meth_s_005fform_005f0(tabColonnes_jsp.java:285)
        at org.apache.jsp.WEB_002dINF.jsp.admin.entreprise.tabEmployes_jsp._jspx_meth_s_005fi18n_005f0(tabColonnes_jsp.java:163)
        at org.apache.jsp.WEB_002dINF.jsp.admin.entreprise.tabEmployes_jsp._jspService(tabColonnes_jsp.java:125)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
        at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
        at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:640)
        at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:634)
        at org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:80)
        ... 166 more
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
@Entity
@Table(name = "ENTREPRISE")
public class Entreprise implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_ENT")
    @SequenceGenerator(name="SEQ_ENT", sequenceName="SEQ_ENT", allocationSize=1)
    @Column(name = "ID_ENT", unique = true, nullable = false, precision = 22, scale = 0)
    private long idEnt;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "entreprise")
    @Fetch(value = FetchMode.SUBSELECT)
    @OrderBy(value="idEmp")
    private List<Employe> listeEmployes;
 
   // getters and setters
}
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
 
@Entity
@Table(name = "EMPLOYE")
public class Employe implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_EMP")
    @SequenceGenerator(name="SEQ_EMP", sequenceName="SEQ_EMP", allocationSize=1)
    @Column(name = "ID_EMP", unique = true, nullable = false, precision = 22, scale = 0)
    private long idEmp;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ID_ENT")
    private Entreprise entreprise;
    @Column(name = "NOM", nullable = false)
    private String nom;
 
    // Autres champs champs textes
 
   // gets and setters
}
Je voudrais accéder à la liste d'employés d'un entreprise à partir d'une jsp comme ceci :
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
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="C" %>
<html>
<body>
	<table>
        <s:iterator value="entreprise.listeEmployes" status="st">
            <s:hidden name="entreprise.listeEmployes[%{#st.index}].idEmp"/>
			<tr>
				<td>
					<s:property value="nom"/>                                
				</td>
			</tr>
		</s:iterator>
	</table>
</body>
</html>
J'ai beau configurer mon fichier web.xml pour prendre en compte le pattern Session-In-View l'exception se produit malgré tout :
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <description>Application Finance</description>
    <display-name>Test d'application</display-name>
 
    <!-- Démarrage de Spring -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
    <listener>
         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
 
    <!-- Configuration de log4J -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>
 
    <!-- Extensions de configuration Spring -->
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/conf/spring/applicationContext*.xml</param-value>
	</context-param>
 
    <filter>
      <filter-name>springSecurityFilterChain</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
 
    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
 
 
    <filter>
        <filter-name>Hibernate Session In View Filter</filter-name>
           <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Hibernate Session In View Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
 
	<!-- Struts 2 config -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
    <!-- Tiles2 -->
    <servlet>
		<servlet-name>tiles</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>tiles</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
 
 
    <session-config>
        <session-timeout>45</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>