Bonjour,

J'essaye d'utiliser Hibernate version 4.3.11.Final et spring version 4.2.2.RELEASE mais quand je récupère la session dans ma classe UserDao j'ai une exception : "No Session found for current thread".
Je ne comprends pas pourquoi je pense avoir rien oublié...

Ci-dessous mes sources XML et java.

Merci d'avance pour votre aide.

web.xml
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
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>
 
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 
<!-- Spring Security -->  
 <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>
root-context.xml
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
 
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:8889/flosite" />
    <property name="username" value="test" />
    <property name="password" value="" />
</bean>
 
<!-- Hibernate Session Factory -->
 
 
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan">
        <array>
            <value>com.rg.flosite.model</value>
            <value>com.rg.flosite.dao</value>
            <value>com.rg.flosite.service</value>
        </array>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
        </value>
    </property>
</bean>
 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
      <property name="sessionFactory" ref="sessionFactory"/>
</bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>
servlet-context.xml
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
 
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
 
<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles.xml</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="preparerFactoryClass"
        value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" />
</beans:bean>
 
<beans:bean id="userService" class="com.rg.flosite.service.UserServiceImpl"></beans:bean>
<beans:bean id="userDao" class="com.rg.flosite.dao.UserDAOImpl"></beans:bean>
<context:component-scan base-package="com.rg.flosite.controller" />
userDao.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
package com.rg.flosite.dao;
 
import com.rg.flosite.model.User;
 
public interface UserDAO {
    public void updateUser(User user);
    public User getUser();
}
UserDAOImpl.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
 
package com.rg.flosite.dao;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
 
import com.rg.flosite.model.User;
 
@Repository
public class UserDAOImpl implements UserDAO {
    @Autowired
    private SessionFactory sessionFactory;
 
    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
 
    @Override
    public void updateUser(User user) {
        User userToUpdate = getUser();
        if (userToUpdate == null) {
            user.setId((long) 1);
            getCurrentSession().save(user);
        }
        else {
            user.setId(userToUpdate.getId());
            getCurrentSession().update(user);
        }
    }
 
    @Override
    public User getUser() {
        User user = (User) getCurrentSession().get(User.class,1);
        return user;
    }
 
}
UserService.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
package com.rg.flosite.service;
 
import com.rg.flosite.model.User;
 
public interface UserService {
    public User getUser();
    public void updateUser(User user);
 
}
UserServiceImpl.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
 
package com.rg.flosite.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.rg.flosite.dao.UserDAO;
import com.rg.flosite.model.User;
 
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDao;
 
    @Override
    public User getUser() {
        return userDao.getUser();
    }
 
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }
 
}
AdminMeControler.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
 
package com.rg.flosite.controller;
 
import java.util.Locale;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.rg.flosite.model.User;
import com.rg.flosite.service.UserService;
 
@Controller
public class AdminMeController {
    @Autowired
    private UserService userService;
    @RequestMapping(value="/admin/moi", method = RequestMethod.GET) 
    public String home(Locale locale,Model model) {
        User user = userService.getUser();
        return "adminme";
    }
 
}