1

我正在使用 Spring 3.0.5、Hibernate 3.3 和generic-hibernate-dao。我已经将 Hibernate SessionFactory 配置如下:

<bean id="sessionFactory"   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean?"> 
    <property name="dataSource">
        <ref local="dataSource" /> 
    </property> 
<property name="packagesToScan" value="com.xxx.re.admin.model" /> <property name="hibernateProperties">

        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect 
            </prop> 
            <prop key="hibernate.show_sql">false</prop> 
            <prop key="hibernate.hbm2ddl.auto">validate</prop> 
        </props> 
    </property> 
</bean> 

<!-- Transaction manager for a single Hibernate SessionFactory? (alternative
    to JTA) --> 
<tx:annotation-driven /> 

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

我创建了一个 BaseDAOImpl 并使用域 DAO 进行扩展,如下所示:

public class BaseDAOImpl<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {
    @Autowired @Override public void setSessionFactory(SessionFactory? sessionFactory) {
        super.setSessionFactory(sessionFactory); 
    } 
}

@Repository public class LocaleDAOImpl extends BaseDAOImpl<Locale, Long> implements LocaleDAO {

}

在访问我的 spring 控制器(调用dao.findAll())时,我收到以下错误:

org.hibernate.HibernateException?: 
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.
4

1 回答 1

0

@Transactional使用(或者如果它是您的控制器和 DAO 之间的中间体,则可能是一些服务)注释您的控制器。服务实际上是一个更好的地方,否则您还需要放置<tx:annotation-driven />*-servlet.xmlMVC 配置文件中。

我从来没有使用过这个库(我个人使用Spring 产品组合中的Spring Data JPA),但文档没有说明任何关于事务的内容,所以我想由用户来配置它们。

更新:查看他们提供的示例似乎我是对的:

@Transactional
public class CitizenServiceImpl implements CitizenService {
//...

http://code.google.com/p/hibernate-generic-dao/source/browse/trunk/sample/jpa-hibernate-maven/src/main/java/sample/googlecode/genericdao/service/CitizenServiceImpl.java? r=635

于 2011-10-30T11:11:21.257 回答