Using Hibernate and Jdbc both in Spring Framework 3.0?

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 .

Up vote 1 down vote favorite 2 share g+ share fb share tw.

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! Java spring hibernate jdbc link|improve this question asked Mar 5 at 18:53Nandkumar1,05113 77% accept rate.

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.

– Nandkumar Mar 6 at 6:23 With spring jdbc you don't need to get connection or manage it manually, you provide callbacks(rowmappers) to handle data. It is best to refer to excellent spring documentation for explanation and examples. – mrembisz Mar 6 at 8:55 thanks mrembisz!

– Nandkumar Mar 7 at 10:24 what about transaction manager? I have used HibernateTransactionManager. – Nandkumar Mar 12 at 7:48 1 HibernateTransactionManager does basically everything DataSourceTransactionManager is doing, plus it manages hibernate sessions.

Both use the same underlying mechanisms to bind transaction+connection to the thread. – mrembisz Mar 12 at 8:49.

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 : java2s.com/Code/Java/JPA/UsingNamedNativ... Scroll a little bit down and find File: Professor. Java You can use this example.

This way you can fire native queries from hibernate.

Thanks Geek! But I want to use simple JDBC. – Nandkumar Mar 5 at 19:35 okay how about creating Data Access Layer(DAO are objects, I am suggesting to create a custom layer) so that Hibernate capabilities and Native JDBC can be used according to need.

It would be something like you have a this another layer (interface) which has some set of methods to perform operations you would like to get done using hibernate capabilities like CRUD , you also support building custom hibernate query (OR, Between, AND etc) and you have method to take simple JDBC queries and it returns result set(Recordset) – Geek Mar 5 at 20:33 Are you going to have multiple implementations of the same DAOs? Ie: PersonDaoHibernate and PersonDaoJdbc? – BrandonV Mar 5 at 21:07 @BrandonV so we have DAO (the plain classes mapping to tables) which will have all the hibernate annotations.

Then another layer data access layer where classes are created using Generic templates and perform operations on T as in (class template). Also may be the same class or like you said PersonalDaoJDBC can be there which can have jdbc querying mechanism. – Geek Mar 5 at 21:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions