简体   繁体   English

在Spring Framework 3.0中使用Hibernate和Jdbc

[英]Using Hibernate and Jdbc both in Spring Framework 3.0

I am working on a project which uses Spring 3.0, hibernate 3.0. 我正在开发一个使用Spring 3.0,hibernate 3.0的项目。 My project has Controller, Service and DAO layers. 我的项目有Controller,Service和DAO层。 Daos(written in hibernate) are accessed from service layer. 从服务层访问Daos(用hibernate编写)。 Now the requirement is I need to use both Hibernate as well as JDBC. 现在要求是我需要同时使用Hibernate和JDBC。 Hibernate part is about 80-90%. Hibernate部分约占80-90%。 For Remaining 10%, I have to use simple JDBC(JdbcTemplate can be used). 对于剩余的10%,我必须使用简单的JDBC(可以使用JdbcTemplate)。 Please suggest me, how do I go for both hibernate and Jdbc together. 请建议我,如何同时使用hibernate和Jdbc。 I need design with Separation, Re-usability. 我需要使用Separation,Re-usability进行设计。 Also how do implement it in Spring? 如何在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 . 您可以安全地将hibernate与spring JDBC混合使用,并且两者都应该能够共享由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. 请记住,您应该使用spring模板来实现此目的,因为它们能够检测并重用与活动事务的线程绑定连接。 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 . 如果由于某种原因你想在混合中添加另一个基于jdbc的库(比如groovy sql),你仍然可以通过spring DataSourceUtils

The only potential issues can arise when both hibernate and spring jdbc templates operate on the same data. 当hibernate和spring jdbc模板对相同数据进行操作时,可能会出现唯一的潜在问题。 Hibernate may delay database updates and spring jdbc would then access outdated data. Hibernate可能会延迟数据库更新,然后spring jdbc会访问过时的数据。 My experience with this interaction comes from older versions of spring and there may be some mechanisms to resolve this issue nowadays. 我对这种交互的体验来自旧版本的spring,现在可能有一些机制可以解决这个问题。

I am writing this answer in hope that people will improve mine answer as this may not be the best answer so please comment. 我正在写这个答案,希望人们能改进我的答案,因为这可能不是最佳答案,所以请评论。

In hibernate you can use named native query example : http://www.java2s.com/Code/Java/JPA/UsingNamedNativeQuery.htm Scroll a little bit down and find File: Professor.java You can use this example. 在hibernate中,您可以使用命名的本机查询示例: http//www.java2s.com/Code/Java/JPA/UsingNamedNativeQuery.htm向下滚动一下,找到File:Professor.java您可以使用此示例。 This way you can fire native queries from hibernate. 这样您就可以从hibernate中激活本机查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM