Spring and hibernate: No Session found for current thread

后端 未结 6 1500
生来不讨喜
生来不讨喜 2020-11-28 11:05

im gettting the following error

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionCont         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 11:52

    I know this is an question with some age, but I ran into this issue and found that if you are using Spring-Java configuration, that a resolution had a couple parts to this. Relative placement of some configuration to the controller was important.

    First, the CoreConfiguration

    @Configuration
    public class CoreConfiguration {
    @Bean          
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean factoryBean = new org.springframework.orm.hibernate4.LocalSessionFactoryBean();
        String annotatedPckgs[] ={"org.tigersndragons.reports.model.warehouse"};
        factoryBean.setAnnotatedPackages(annotatedPckgs);           
        Properties hibernateProperties = new Properties();
        try {
            hibernateProperties.load(this.getClass().getResourceAsStream("props/hibernate.properties"));        
            factoryBean.setHibernateProperties(hibernateProperties);
        } catch (IOException e) { }
        factoryBean.setPackagesToScan("org.telligen.reports.model.warehouse");
        factoryBean.setDataSource(warehouseDataSource());//("jdbc/warehouse");
        try {
            factoryBean.afterPropertiesSet();
        } catch (IOException e) {       }
        return factoryBean;
    }
    @Bean
    public WarehouseDAO getWarehouseDAO(){
        WarehouseDAO wrhsDao = new WarehouseDAO();
        wrhsDao.setSessionFactory(sessionFactory().getObject());
        return wrhsDao;
    }
    

    ...

    @Configuration
    public class ScheduleConfiguration {
    private static Logger logger = LoggerFactory.getLogger(ScheduleConfiguration.class);
    
    @Autowired
    private CoreConfiguration coreConfiguration;
    
    
    @Bean
    public HandlerMapping handlerMapping(){
        DefaultAnnotationHandlerMapping mapping =  new DefaultAnnotationHandlerMapping();
        mapping.setInterceptors(new Object []{coreConfiguration.openSessionViewInterceptor()});
        return mapping;
    }
    
    @Bean
    public HandlerAdapter handerAdapter(){
        return new AnnotationMethodHandlerAdapter();
    }
    
    @Bean
    public ScheduleController scheduleController() throws Exception{
        ScheduleController controller = new ScheduleController();
            controller.setWrhsDao(coreConfiguration.getWarehouseDAO());
        return controller;
    }
    
     ...
    

    In the Controller, I had to set

    @Controller
    @RequestMapping
    public class ScheduleController {
    private static Logger logger = LoggerFactory.getLogger(ScheduleController.class);
    
    private WarehouseDAO wrhsDao;
        @RenderMapping
    @RequestMapping("VIEW")
    public String viewSchedule(Map modelMap){...}
    
    public void setWrhsDao(WarehouseDAO wrhsDao) {
        this.wrhsDao = wrhsDao;
    }
    }
    

    The WarehouseDAO has the @Repository annotation and the SessionFactory was not Autowired.

    Hope this helps someone else with similar questions.

提交回复
热议问题