没有 Hibernate 会话绑定到带有 generic-hibernate-dao 库的线程
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 @Transactional 注释您的控制器(或者如果它是控制器和 DAO 之间的中间体,则可能是某些服务)。 Service 实际上是一个更好的地方,否则你还需要将
放在*-servlet.xml
MVC 配置文件中。我从未使用过这个库(我个人使用 Spring 产品组合中的 Spring Data JPA),但是该文档没有提及任何有关事务的内容,因此我想由用户来配置它们。
更新:看看他们提供的示例似乎我是对的:
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:
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