33

我在 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();
 }
}

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

4

4 回答 4

45

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

也可以看看:

于 2012-05-05T09:09:09.977 回答
25

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

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

<tx:annotation-driven/> 

<bean id="transactionManager" 
 class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
于 2013-04-30T09:38:15.403 回答
6

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

我将一些@Transactional 放在“受保护”的上并收到此错误。

希望能帮助到你 :)

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

方法可见性和@Transactional

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

于 2014-04-01T19:26:05.970 回答
-1

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

于 2012-05-05T08:17:09.480 回答