【问题标题】:AppFuse - Spring Hibernate - no session found for the current threadAppFuse - Spring Hibernate - 没有为当前线程找到会话
【发布时间】:2013-06-21 12:03:14
【问题描述】:

这是我的问题:我开始测试 AppFuse 以了解它是否符合我的需求。

所以我开始创建一个项目(AppFuse 不是 Light 版本,不是多模块项目)。

然后我按照持久性章节中的步骤(使用 Hibernate)创建了我的 bean、daos 和管理器。

第一次尝试:按照指南中的建议,我只是使用 AppFuse 辅助类(GenericDaoHibernate 和 GenericManager)配置了 dao 和 manager。一切正常。

第二次尝试:我需要创建一个自定义 Dao 和一个自定义 Manager 来处理我的特定业务规则......再次按照教程......现在我遇到了这个让我抓狂的问题:

> 2013-06-24
> 21:42:52.512:WARN:oejs.ServletHandler:/certificati/fornitore
> 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:978)
>   at
> org.appfuse.dao.hibernate.GenericDaoHibernate.getSession(GenericDaoHibernate.java:86)
>   at
> org.appfuse.dao.hibernate.GenericDaoHibernate.getAll(GenericDaoHibernate.java:104)
>   at
> org.appfuse.service.impl.GenericManagerImpl.getAll(GenericManagerImpl.java:71)
>   at
> com.alessandrodonato.webapp.controller.FornitoreController.handleRequest(FornitoreController.java:57)

为什么?

我将所有的类(控制器、dao、bean、管理器)放在同一个包.webapp(.webapp.controller、*.webapp.dao、*.webapp.dao.impl、抄送)

我的 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-3.0.xsd
                                                    http://www.springframework.org/schema/context 
                                                    http://www.springframework.org/schema/context/spring-context.xsd
                                                    http://www.springframework.org/schema/tx
                                http://www.springframework.org/schema/tx/spring-tx.xsd"
           default-lazy-init="true">

        <!-- Activates scanning of @Autowired -->
        <context:annotation-config/>

        <!-- Activates scanning of @Repository and @Service -->
        <context:component-scan base-package="com.alessandrodonato"/>

        <!-- Add new DAOs here -->      <!-- <bean id="fornitoreDao" class="org.appfuse.dao.hibernate.GenericDaoHibernate">
            <constructor-arg value="com.alessandrodonato.webapp.model.Fornitore"/> 
        </bean> -->

        <tx:annotation-driven/>

        <!-- Add new Managers here -->      <bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
                <property name = "sessionFactory" ref = "sessionFactory" /> 
     </bean>    
  </beans>

我的调度程序-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       default-lazy-init="true">

    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.dao.DataAccessException">
                    dataAccessFailure
                </prop>
            </props>
        </property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="2097152"/>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="ApplicationResources"/>
        <property name="useCodeAsDefaultMessage" value="true"/>
    </bean> 

    <context:component-scan base-package="com.alessandrodonato.webapp"/>

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven/>

    <!-- Convenient way to map URLs to JSPs w/o having a Controller -->
    <mvc:view-controller path="/admin/activeUsers" view-name="admin/activeUsers"/>
    <mvc:view-controller path="/mainMenu" view-name="mainMenu"/>

    <!-- View Resolver for JSPs -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="requestContextAttribute" value="rc"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- Add additional controller beans here -->

</beans>

我的头疼在长大……

【问题讨论】:

  • 你的 FornitoreController 是什么样的?换句话说,你是如何注入 GenericManager 的?
  • 如果你使用 Autowired 而不是 Resource,它可以工作吗?
  • 正如您在控制器中看到的,我在 Resource 之前使用了 Autowired。但它不起作用。
  • 你可以尝试使用appfuse的生成器。 (在 pom 文件 amp.genericCore 中设置为 false,尽管我现在遇到了接线问题 :(

标签: spring hibernate spring-mvc appfuse


【解决方案1】:

这可能在一年多以前就有人问过了,但我希望我能帮助别人。

我在开始使用 appfuse 框架时遇到了类似的问题。事实证明,我一直忘记在我的自定义管理器实现中添加单参数构造函数。我认为,最重要的部分是将 autowired dao 实例 传递给父类。

预期的自定义管理器实现示例:

@Service("customManager")
public class CustomManagerImpl extends GenericManagerImpl 实现 CustomManager {
    私人 UserDao userDao;

    @Autowired
    public CustomManagerImpl(UserDao userDao){
        超级(userDao);
        this.userDao = userDao;
    }
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2015-05-28
    • 2014-01-10
    • 1970-01-01
    • 2014-08-16
    • 2012-05-05
    • 2012-05-14
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多