IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

Problème "No Session found for current thread" [Integration]


Sujet :

Spring Java

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 28
    Par défaut Problème "No Session found for current thread"
    Bonjour,

    Je réalise une application web ou j'utilise éclipse indigo, tomcat 7.0, Hibernate 4.1.1 et Spring 3.1.1.

    Lorsque je veux tester une de mes classes avec JUnit, j'obtiens l'erreur :

    org.hibernate.HibernateException: No Session found for current thread

    J'en déduit que ma session n'est pas crée. Peut être que cela vient de la commande getCurrentSession().

    voici mes fichiers :

    application-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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
     
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    		<property name="url" value="jdbc:mysql://localhost/hibernate"/>
    		<property name="username" value="root"/>
    		<property name="password" value="admin"/>
    	</bean>
     
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="annotatedClasses">
    			<list>
    				<value>App.Modele.FieldOwnedRecords</value>
    				<value>App.Modele.OwnRecord</value>
    				<value>App.Modele.Updatehistory</value>
    				<value>App.Modele.Evaluation</value>
    				<value>App.Modele.Soundtrack</value>
    				<value>App.Modele.Playlist</value>
    				<value>App.Modele.Comment</value>
    				<value>App.Modele.Field</value>
    				<value>App.Modele.Artist</value>
    				<value>App.Modele.AuthenticatedUser</value>
            		<value>App.Modele.Record</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>	
    			</props>
    		</property>
    	</bean>
     
    	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
     
    	<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
    	<context:annotation-config/>
    	<context:component-scan base-package="App"></context:component-scan>
    </beans>
    classe à tester:

    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
    package App.Service;
     
     
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import App.Modele.AuthenticatedUser;
     
    @Service("UserService")
    @Transactional
    public class UserServiceImpl implements UserService {
     
    	@Autowired
    	private SessionFactory sessionFactory;
     
     
    	public boolean login(String username, String password) {
    		Object[] params = {username, password};
    		return sessionFactory.getCurrentSession().createFilter(params,"From authenticated_user Where username=? and password=?") != null;
    	}
     
    	@Override
    	public void saveUser(AuthenticatedUser user) {
    		sessionFactory.getCurrentSession().save(user);
    	}
     
    	@Override
    	public void updateUser(AuthenticatedUser user) {
    		sessionFactory.getCurrentSession().update(user);
    	}
     
    	@Override
    	public AuthenticatedUser userProfile(String username) {
    	return (AuthenticatedUser) sessionFactory.getCurrentSession().load(AuthenticatedUser.class,username);
     
    	}
     
    	public SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
     
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
     
     
    }
    classe de test :

    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
    import static org.junit.Assert.*;
     
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import App.Service.UserService;
     
     
    public class ServiceTest {
     
    	private static ClassPathXmlApplicationContext context;
    	private static UserService userService;
     
    	@BeforeClass
    	public static void setUpBeforeClass() throws Exception {
    		context=new ClassPathXmlApplicationContext("application-context.xml");
    		userService = (UserService) context.getBean("UserService");
    	}
     
    	@AfterClass
    	public static void tearDownAfterClass() throws Exception {
    		context.close();
    	}
     
    	@Test
    	public void testLogin() {
    		userService.login("root", "admin");
    	}
     
    	@Test
    	public void testSaveUser() {
    		fail("Not yet implemented");
    	}
     
    	@Test
    	public void testUpdateUser() {
    		fail("Not yet implemented");
    	}
     
    	@Test
    	public void testUserProfile() {
    		fail("Not yet implemented");
    	}
     
    }
    Merci d'avance

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 141
    Par défaut
    Déjà Spring, t'affiche pas des erreurs? d'injection de la SessionFactory tout ça? Pour moi c'est tout à fait normal, t'as aucune SessionFactory créée. LocalSessionFactoryBean n'est pas une implémentation de la SessionFactory. Donc t'as zéro session. Dans ta classe UserServiceImpl, à la place de SessionFactory, utilises l'injection sur LocalSessionFactoryBean, puis tu peux créer une méthode comme ceci.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    @Autowired priavate LocalSessionFactoryBean bean;
    public SessionFactory getSessionFactory(){
    return bean.getObject();
    }
    Sinon crée ta SessionFactory dans le contexte de Spring pour dirrectement l'injecter.

  3. #3
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2012
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2012
    Messages : 2
    Par défaut no session found for current thread
    Essaye d'annoter la méthode avec @Transactional comme mentionné dans cette discussion : http://developerfirm.com/topic/12/no...current-thread

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 03/06/2014, 16h30
  2. Réponses: 1
    Dernier message: 30/01/2011, 21h26
  3. Problèmes avec les Sessions
    Par alexthomas dans le forum Langage
    Réponses: 5
    Dernier message: 20/11/2005, 21h53
  4. Problème avec les sessions
    Par philippef dans le forum Langage
    Réponses: 2
    Dernier message: 27/10/2005, 15h19
  5. WEB SERVICE No serializer found for class
    Par lch dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 14/09/2005, 16h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo