没有 Hibernate 会话绑定到带有 generic-hibernate-dao 库的线程

发布于 2024-12-12 17:11:20 字数 1942 浏览 1 评论 0原文

我正在使用 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.

I'm working with Spring 3.0.5,Hibernate 3.3 and generic-hibernate-dao. I've configured Hibernate SessionFactory as below:

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

I've created a BaseDAOImpl and extended with a domain DAO as below:

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 {

}

On accessing my spring controller (calling the dao.findAll()) I get the following error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2024-12-19 17:11:20

使用 @Transactional 注释您的控制器(或者如果它是控制器和 DAO 之间的中间体,则可能是某些服务)。 Service 实际上是一个更好的地方,否则你还需要将 放在 *-servlet.xml MVC 配置文件中。

我从未使用过这个库(我个人使用 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/服务/CitizenServiceImpl.java?r=635

Annotate your controller with @Transactional (or maybe some service if it is an intermediate between your controller and DAO). Service is actually a better place, otherwise you need to place <tx:annotation-driven /> in *-servlet.xml MVC configuration file as well.

I have never used this library (I personally use Spring Data JPA from Spring portfolio), but the documentation doesn't say anything about transactions, so I guess it is up to the user to configure them.

UPDATE: looking at the examples they provide seems like I am right:

@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文