Bonjour,

Je travaille sur un projet de portlet avec Spring MVC 3 et Hibernate, et je suis confronté à un probleme que je ne parviens pas à résoudre.

J'ai des tables reliées entre elles via de multiples associations ManyToOne et OneToMany.

Mon probleme est que je souhaite accéder à des champs des objets représentant ces tables via lazy loading dans la vue de mon portlet. Jusque la, pour éviter la fameuse LazyInitializationException, je force le chargement de mes objets a base de requetes supplémentaires (du eager loading barbarre quoi ....) C'est pas beau, je sais.

Pour éviter cela, je pense qu'il faut que j'étende la durée de vie de ma session Hibernate à toute la durée de traitement de ma requete, pour pouvoir conserver l'avantage du lazy loading dans la vue

C'est la que ca coince. Je n'arrive pas à mettre en place le pattern OSIV (OpenSessionInView) dont tout le monde parle sur le net, et j'ai besoin d'un coup de main à ce niveau la.

Je vous poste quelques segments de code ou j'ai peut etre fait n'importe quoi:

web.xml
Code xml : 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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>monportlet-portlet</display-name>
  <jsp-config>
    <taglib>
      <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
      <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
    </taglib>
  </jsp-config>
 
  <servlet>
    <servlet-name>ViewRendererServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewRendererServlet</servlet-name>
    <url-pattern>/WEB-INF/servlet/view</url-pattern>
  </servlet-mapping>
</web-app>

monportlet-portlet.xml
Code xml : 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
<!-- Activer la configuration des beans par annotations -->
    <context:annotation-config />
    <context:component-scan base-package = "monpackage"/>
 
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/view/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
 
	<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
 
 
	<!-- OPEN SESSION IN VIEW  -->
	<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
		<property name="sessionFactory" ref="mySessionFactory"/>
	</bean>
 
	<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">
	       <!-- <list>-->
	           <ref bean="openSessionInViewInterceptor"/>
	       <!-- </list> -->
	   </property>	   
   </bean>
 
 
	<!-- HIBERNATE CONFIGURATION -->		
 
	<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost/mon_portlet?useUnicode=true" />  
        <property name="username" value="root" />  
        <property name="password" value="" />  
    </bean> 
 
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="monpackage"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="javax.persistence.validation.mode">NONE</prop>
            </props>
        </property>
    </bean>
 
	<bean id="txManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="dataSource" ref="myDataSource" />
		<property name = "sessionFactory" ref = "mySessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
 
	<bean id="myContentDAO" class="monpackage.dao.hibernate.ContentDAOHibernateImpl" />
 
	<bean id="myGroupDAO" class="monpackage.dao.hibernate.GroupDAOHibernateImpl" />
 
	<bean id="myPublicationDAO" class="monpackage.dao.hibernate.PublicationDAOHibernateImpl" />

Mes classes de modele:

Content.java
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
@Entity(name="MyContent")
@Table(name="content_tbl")
@Inheritance(strategy=InheritanceType.JOINED)
public class Content implements Serializable
{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
	private Long id;
 
	@Column(name="description", nullable=false)
	private String description;
 
	@ManyToOne
	private Publication publication;
 
	public Content()
       {
	    super();
	}
 
    public Content(Publication publication, String description)
    {
		this();
	    this.setPublication(publication);
	    this.setDescription(description);
    }
 
 
           .... getters/setters ...
}
Group.java
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
@Entity(name="MyGroup")
@Table(name="group_tbl")
public class Group implements Serializable
{
	@Id
	@Column(name="id")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
 
	@Column(name="authorId", nullable=false)
	private Long authorId;
 
	@Column(name="name", nullable=false)
	private String name;
 
	@Column(name="description", nullable=false)
	private String description;
 
	@Column(name="isActive", nullable=false)
	private boolean isActive;
 
	@OneToMany
	@JoinTable(name = "group_publication")
	private Collection<Publication> publications = new ArrayList<Publication>();
 
	public Group()
    {
	    super();
    }
 
    public Group(Long author_id, String name, String description, boolean is_active)
    {
	    this();
	    this.setAuthorId(author_id);
	    this.setName(name);
	    this.setDescription(description);
	    this.setActive(is_active);
    }
 
 
           .... getters/setters ...
 
}
Publication.java
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
@Entity(name="MyPublication")
@Table(name="publication_tbl")
public class Publication implements Serializable
{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
	private Long id;
 
	@Column(name="authorId")
	private Long authorId;
 
    @ManyToOne
    private Group group;
 
	@Column(name="publicationDatetime", nullable=false)
	private Date publicationDatetime;
 
	@Column(name="isActive", nullable=false)
	private boolean isActive;
 
	@OneToMany
	@JoinTable(name = "publication_content")
	private Collection<Content> contents = new ArrayList<Content>();
 
 
	@OneToMany
	@JoinTable(name = "publication_answer")
	private Collection<Publication> answers = new ArrayList<Publication>();
 
	@ManyToOne
	private Publication parentPublication = null;
 
	@Transient // pour que hibernate ignore le champ
	private User author = null;
 
	public Publication()
    {
	    super();
    }
 
    public Publication(Long author_id, Group group, Date publication_datetime, boolean is_active)
    {
	    this();
	    this.setAuthorId(author_id);
	    this.setGroup(group);
	    this.setPublicationDatetime(publication_datetime);
	    this.setActive(is_active);
    }
 
 
           .... getters/setters ...
 
}
Merci d'avance de votre aide.