【问题标题】:Hibernate Exception : No Session found for current thread休眠异常:没有为当前线程找到会话
【发布时间】:2014-06-17 06:32:55
【问题描述】:

我有一个使用 Hibernate 开发 Spring 的应用程序。一切都很顺利,但是当我尝试使用休眠类测试我的事务时,它会抛出一个错误,说“org.hibernate.HibernateException: No Session found for current thread”。我找不到我的错误。非常感谢您的帮助。

我的主要课程:

 public static void main(String[] args) {

        Patient sac = new Patient();
        sac.setPatient_Id("M2314");
        sac.setPatient_Age(23);
        sac.setPatient_Name("Sac");
        sac.setPatient_Address("SGTY NY");


        System.out.println("load context");
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");

        PatientService patientService = (PatientService) context.getBean("patientService");
        patientService.persistPatient(sac);

        context.close();
    }

serviceImpl 的一部分:

@Service("patientService")
public class PatientServiceImpl implements PatientService{

    @Autowired
    PatientDAO patientdao;

    @Override
    @Transactional
    public void persistPatient(Patient patient) {
        patientdao.persistPatient(patient);

    }

DAO 类的一部分:

@Repository("patientdao")
public class PatientDAOImpl implements PatientDAO{

    @Autowired
    private SessionFactory sessionFactory;


    @Override
    @Transactional
    public void persistPatient(Patient patient) {
        sessionFactory.getCurrentSession().persist(patient);

    }

我的 servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />


    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.tela.pms" />

   <beans:bean id="patientService" class="com.tela.pms.service.impl.PatientServiceImpl"/>
   <beans:bean id="patientdao" class="com.tela.pms.dao.impl.PatientDAOImpl"/>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="root" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"></beans:property>
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.tela.pms.domain.Patient</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory">
    </beans:bean>


</beans:beans>

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    在这种情况下,我不确定您是否启用了annotation-driven 事务管理,因为您没有提供 SpringBean.xml 文件的代码。

    你需要在你的servlet-context.xml中添加这个标签&lt;tx:annotation-driven/&gt;标签

    还请确保将其指向您的事务管理器,如下所示

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

    您的代码的其余部分看起来不错,并且此错误应该会随着上述更改而消失。

    【讨论】:

    • 没有什么可以取消评论的。需要添加它。并且没有 SpringBean.xml
    • 在早期版本中,此属性存在于 SpringBean.xml 中,但正如您在帖子中提到的那样,在 servlet-context.xml 中添加此标记 将解决问题。编辑答案以反映这一点。
    【解决方案2】:

    你有@Transactional 时间和地点,但没有方法。添加&lt;tx:annotation-driven /&gt;,开启@Transactional的注解处理。

    【讨论】:

    • 您认为@Transactional 是否有助于摆脱无会话问题?
    • 您需要事务,您有 @Transactional 但目前没有任何作用,因为您还没有配置任何东西来处理注释。简而言之,它会解决您的问题。
    • tutorialspoint.com/hibernate/hibernate_sessions.htm 尝试从会话工厂创建会话,然后使用事务。
    • @Saif 用于手动 tx 管理而不是声明性 tx 管理。对于任何相当大的应用程序,手动 tx 都是不可用的。
    【解决方案3】:

    添加将是一个解决方案,但我得到了另一个与此相关的解决方案。所以我不得不在我的 servlet-context.xml 中做这个改变(部分在这里):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing 
            infrastructure -->
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <annotation-driven />
    
        <tx:annotation-driven />
    

    变化:

    添加&lt;tx:annotation-driven /&gt;. 并注意在beans:beans 声明中添加http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdxsi:schemaLocation。然后就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 1970-01-01
      • 2012-05-14
      • 2015-05-28
      • 2014-09-29
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多