【问题标题】:org.hibernate.HibernateException: No Session found for current threadorg.hibernate.HibernateException:没有为当前线程找到会话
【发布时间】:2012-05-14 15:40:10
【问题描述】:

Spring3 和 Hibernte4 出现上述异常

以下是我的 bean xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

   <context:annotation-config/>


   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/GHS"/>
      <property name="username" value="root"/>
      <property name="password" value="newpwd"/>
  </bean>

  <bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.example.ghs.model.timetable</value>
        </list>
    </property>
  </bean>

   <bean id="baseDAO"
      class="com.example.ghs.dao.BaseDAOImpl"/>
</beans>

我的 BaseDAO 类看起来像这样

public class BaseDAOImpl implements BaseDAO{
private SessionFactory sessionFactory;

 @Autowired
 public BaseDAOImpl(SessionFactory sessionFactory){
     this.sessionFactory = sessionFactory;
 }

 @Override
 public Session getCurrentSession(){
     return sessionFactory.getCurrentSession();
 }
}

以下代码抛出标题中的异常

public class Main {
 public static void main(String[] args){
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("dao-beans.xml");
    BaseDAO bd = (BaseDAO) context.getBean("baseDAO");
    bd.getCurrentSession();
 }
}

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: spring hibernate spring-3 hibernate-4.x


    【解决方案1】:

    您将 BaseDAOImpl 类放入哪个包中。我认为它需要一个类似于您在应用程序上下文 xml 中使用的包名称,并且它也需要相关注释。

    【讨论】:

      【解决方案2】:

      getCurrentSession() 仅在事务范围内才有意义。

      您需要声明一个适当的事务管理器,划定事务的边界并在其中执行数据访问。例如如下:

      <bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name = "sessionFactory" ref = "sessionFactory" />
      </bean>
      

      .

      PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);
      TransactionTemplate tx = new TransactionTemplate(ptm);
      
      tx.execute(new TransactionCallbackWithoutResult() {
          public void doInTransactionWithoutResult(TransactionStatus status) { 
              // Perform data access here
          }
      });
      

      另请参阅:

      【讨论】:

      • 感谢您的回答。我会看看你上面链接的资源。
      • &lt;tx:annotation-driven/&gt; 也应包含在@vishal-jagtap 中提到的内容中
      • 非常感谢,这真的帮助了我前进。
      • 谢谢。这有帮助。我只需要添加@Transactional 注释使其成为事务范围。
      【解决方案3】:

      我遇到了同样的问题并得到了如下解决 在 daoImpl 类上添加了@Transactional

      在配置文件中添加了 trnsaction 管理器:

      <tx:annotation-driven/> 
      
      <bean id="transactionManager" 
       class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>
      

      【讨论】:

        【解决方案4】:

        我将添加一些我花了一些时间进行调试的内容:不要忘记@Transactional 注释仅适用于“公共”方法。

        我把一些@Transactional 放在“受保护”的上面,然后得到了这个错误。

        希望对你有帮助:)

        http://docs.spring.io/spring/docs/3.1.0.M2/spring-framework-reference/html/transaction.html

        方法可见性和@Transactional

        当使用代理时,你应该应用@Transactional注解 仅适用于具有公共可见性的方法。如果您确实注释受保护, 带有 @Transactional 注释的私有或包可见方法, 没有引发错误,但带注释的方法没有表现出 配置的事务设置。考虑使用 AspectJ(参见 下面)如果您需要注释非公共方法。

        【讨论】:

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