org.hibernate.HibernateException: No Session found

2019-01-13 07:56发布

问题:

I'm getting the above exception with Spring3 and Hibernte4

The following is my bean xml file

<?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

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?

回答1:

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

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:

  • 10. Transaction Management
  • 13.3 Hibernate


回答2:

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

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>


回答3:

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.

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

Hope it helps :)

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.



回答4:

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.