spring org.hibernate.HibernateException: 找不到当前线程的会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10459922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:06:57  来源:igfitidea点击:

org.hibernate.HibernateException: No Session found for current thread

springhibernatespring-3hibernate-4.x

提问by Can't Tell

I'm getting the above exception with Spring3 and Hibernte4

我在 Spring3 和 Hibernte4 中遇到了上述异常

The following is my bean xml file

以下是我的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>

My BaseDAO class looks like this

我的 BaseDAO 类看起来像这样

public class BaseDAOImpl implements BaseDAO{
private SessionFactory sessionFactory;

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

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

The following code throws the exception in the title

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

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

Does anyone have an idea about how to solve this problem?

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

回答by axtavt

getCurrentSession()only makes sense inside a scope of transaction.

getCurrentSession()只有在交易范围内才有意义。

You need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows:

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

<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
    }
});

See also:

也可以看看:

回答by Vishal Jagtap

I came across same problem and got solved as below Added @Transactional on daoImpl class

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

Added trnsaction manager in configuration file:

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

<tx:annotation-driven/> 

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

回答by lboix

I'll just add something that took me some time to debug : don't forget that a @Transactional annotation will only work on "public" methods.

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

I put some @Transactional on "protected" ones and got this error.

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

Hope it helps :)

希望能帮助到你 :)

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

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

Method visibility and @Transactional

When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.

方法可见性和@Transactional

使用代理时,您应该仅将 @Transactional 注释应用于具有公共可见性的方法。如果您使用 @Transactional 注释对受保护的、私有的或包可见的方法进行注释,则不会引发错误,但带注释的方法不会显示配置的事务设置。如果您需要注释非公共方法,请考虑使用 AspectJ(见下文)。

回答by Kush Sharma

Which package u have put the BaseDAOImpl class in.. I think It requires a package name similar to the one u have used in the application context xml and it requires a relevant annotation too.

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