No Session found for current thread

0

I'm having trouble performing a test to add a profile user! My problem is in the addUser method.

Test class code:

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>

Thank you in advance.

    
asked by anonymous 01.12.2014 / 02:07

0 answers