在Spring Framework 3.0中使用Hibernate和Jdbc [英] Using Hibernate and Jdbc both in Spring Framework 3.0

查看:100
本文介绍了在Spring Framework 3.0中使用Hibernate和Jdbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用Spring 3.0,hibernate 3.0的项目。我的项目有Controller,Service和DAO层。 Daos(用hibernate编写)从服务层访问。现在需求是我需要同时使用Hibernate和JDBC。 Hibernate部分约为80-90%。对于剩余的10%,我必须使用简单的JDBC(可以使用JdbcTemplate)。请建议我,我如何同时使用hibernate和Jdbc。我需要设计分离,可重用性。还有如何在Spring中实现它?



任何建议都将得到满足。



您可以安全地将hibernate与spring JDBC混合使用,并且应该能够共享由 HibernateTransactionManager 管理的事务。

code>。请记住,您应该使用Spring模板来实现此目的,因为它们能够检测并重用活动事务中的线程绑定连接。如果由于某种原因,您想添加另一个基于jdbc的库(例如groovy sql),您仍然可以通过spring DataSourceUtils

完成此操作。 b
$ b

当hibernate和spring jdbc模板都在相同的数据上运行时,可能会出现唯一的潜在问题。 Hibernate可能会延迟数据库更新,然后spring jdbc会访问过时的数据。我对这种交互的经验来自Spring的旧版本,现在可能有一些机制来解决这个问题。


I am working on a project which uses Spring 3.0, hibernate 3.0. My project has Controller, Service and DAO layers. Daos(written in hibernate) are accessed from service layer. Now the requirement is I need to use both Hibernate as well as JDBC. Hibernate part is about 80-90%. For Remaining 10%, I have to use simple JDBC(JdbcTemplate can be used). Please suggest me, how do I go for both hibernate and Jdbc together. I need design with Separation, Re-usability. Also how do implement it in Spring?

Any suggestions are appreciated.

Thanks in Advance!!

解决方案

You can safely mix hibernate with spring JDBC and both should be able to share transactions managed by HibernateTransactionManager. Keep in mind you should be using spring templates to achieve this because they are able to detect and reuse thread-bound connection with active transaction. If for some reason you would like to add another jdbc based library to the mix (like groovy sql for example) you can still do it through spring DataSourceUtils.

The only potential issues can arise when both hibernate and spring jdbc templates operate on the same data. Hibernate may delay database updates and spring jdbc would then access outdated data. My experience with this interaction comes from older versions of spring and there may be some mechanisms to resolve this issue nowadays.

这篇关于在Spring Framework 3.0中使用Hibernate和Jdbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆