No Session found for Current thread

Asked

Viewed 40 times

0

I am having trouble performing a test to add a user with profile! My problem is the method addUser.

Code of the test class:

public class AddUser {

    public static void main(String[] args) {

        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");

        Perfil perfil = (Perfil) appContext.getBean(Perfil.class);
        perfil.setTipo("testePerfil");

        User user = (User) appContext.getBean(User.class);
        user.setAtivo(true);
        user.setCpf("111111111-29");
        user.setEmail("[email protected]");
        user.setNome("teste");
        user.setSenha("12345");

        List<Perfil> listPerfil = new ArrayList<Perfil>();  
        listPerfil.add(perfil); 
        user.setPerfis(listPerfil);

        UserDAO userDao = (UserDAO) appContext.getBean("UserDAO",UserDAO.class);
        userDao.addUser(user); // <<===== ERROR
    }
}

My class UserDao:

public class UserDAO implements IUserDAO, Serializable {

    private static final long serialVersionUID = 1L;

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void addUser(User user) {
        getSessionFactory().getCurrentSession().save(user);
    }
}

My applicationContext:

<bean id="UserDAO" class="com.dao.impl.UserDAO">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>db.properties</value>
    </property>
</bean>

<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/banco" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxPoolSize" value="10" />
    <property name="maxStatements" value="0" />
    <property name="minPoolSize" value="5" />
</bean>

<!-- Session Factory Declaration -->
<bean id="SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.model.User</value>
            <value>com.model.Perfil</value>
            <value>com.model.Cliente</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

Thanks in advance.

  • 1

    Can you explain better what is the error that occurs? If possible by posting a stacktrace or something similar.

  • 1

    Two things. 1. Your code has an annotation @Transactional? 2. There is a hibernate.cfg.xml doing something strange in the classpath of the project?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.