0

I am using Spring MVC, and I want to init a spring bean and during this init I want to get data from database.

[20/mars/15 10:54:44:044 GMT+01:00] ERROR context.ContextLoader: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeMB': Invocation of init method failed; nested exception is org.hibernate.HibernateException: No Session found for current thread at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1505)


Caused by: org.hibernate.HibernateException: No Session found for current thread at

org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024) at com.tds.erp.dao.impl.EmployeeDaoImpl.listEmployee(EmployeeDaoImpl.java:47) at com.tds.erp.services.impl.EmployeeServiceImpl.listEmployee(EmployeeServiceImpl.java:48) at com.tds.erp.managedController.EmployeeMB.init(EmployeeMB.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

ApplicationContext.xml

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />
    <context:component-scan base-package="com.tds.erp" />
    <context:annotation-config />
    <context:spring-configured />


    <!-- Data Source Declaration -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${database.driver}" />

        <property name="url" value="${database.url}" />

        <property name="username" value="${database.user}" />

        <property name="password" value="${database.password}" />

    </bean>
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.tds.erp.model" />
        <!-- <property name="packagesToScan"> <list> <value>net.javabeat.spring.model</value> 
            </list> </property> -->

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <!-- Enable the configuration of transactional behavior based on annotations -->
    <!-- <tx:annotation-driven transaction-manager="txManager"/> -->

    <!-- Transaction Manager is defined -->
    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory"  />
    </bean>
</beans>

ManagedBean.java

@Component
@ManagedBean
@SessionScoped
public class EmployeeMB {



    @Autowired
    private IEmployeeService EmployeService;

    private Employee employee= new Employee();
    private List<Employee> employeeList= new ArrayList<Employee>();
    private Employee selectedEmployee=new Employee();
    private boolean headerButtonsDisabled=true;


    @PostConstruct
    public void init(){
        setEmployeeList(EmployeService.listEmployee());
    }

    public void setEmployeService(IEmployeeService employeService) {
        EmployeService = employeService;
    }

    public IEmployeeService getEmployeService() {
        return EmployeService;
    }

    public Employee getEmployee() {
        return employee;
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Spartan
  • 1,167
  • 4
  • 20
  • 40
  • what is on the line at com.tds.erp.dao.impl.EmployeeDaoImpl.listEmployee(EmployeeDaoImpl.java:47) ? – vincent Mar 20 '15 at 10:30
  • There are no transactions avaijlable in the init method. Also what is your bean a spring component or a JSF managed bean (why the `@ManagedBean` annotation?). Also `@ManagedBean` and `@Component` don't mix they are basically mutally exclusive. – M. Deinum Mar 20 '15 at 10:33
  • I am following the MVC Pattern so should I remove `@ManagedBean` and leave `@Component` ?? – Spartan Mar 20 '15 at 10:48
  • 1
    Get the concepts and terminology right. This is not Spring MVC. Food for read and thought here: http://stackoverflow.com/q/18369356 – BalusC Mar 20 '15 at 10:49
  • I understand that i should remove `@ManagedBean` beacause I am using DI, but stil not resolving the init problem – Spartan Mar 20 '15 at 11:57

0 Answers0