3

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

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85

2 Answers2

7

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.

mrembisz
  • 12,722
  • 7
  • 36
  • 32
  • can you tell me how do I use it? any example for how to get connection and how to use it? – Nandkumar Tekale Mar 06 '12 at 06: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 06 '12 at 08:55
  • what about transaction manager? I have used HibernateTransactionManager. – Nandkumar Tekale Mar 12 '12 at 07:48
  • It is fine. Spring JDBC templates work perfectly with this one. – mrembisz Mar 12 '12 at 08:23
  • how does it work? Actually, what I read in Spring docs is HibernateTransactionManager works with sessionFactory, and DataSourceTransactionManager works with jdbcTemplate/plain jdbc. – Nandkumar Tekale Mar 12 '12 at 08:39
  • 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 '12 at 08:49
  • 1
    Thanks! Also I confirmed this answer at http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/HibernateTransactionManager.html – Nandkumar Tekale Mar 12 '12 at 10:05
  • at above link, it is given that, HibernateTransactionManager will handle hibernate(with single session factory) and plain jdbc. What about when using JdbcTemplate?? Also I want to know about logging queries and performance of the query when using JdbcTemplate? – Nandkumar Tekale Mar 12 '12 at 10:13
  • JdbcTemplate could be what is called plain jdbc there, no worries. You can do some query logging through org.springframework.jdbc, not sure about performance measurements. – mrembisz Mar 12 '12 at 11:23
1

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. This way you can fire native queries from hibernate.

Geek
  • 627
  • 1
  • 8
  • 18
  • 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 05 '12 at 20:33
  • Are you going to have multiple implementations of the same DAOs? ie: PersonDaoHibernate and PersonDaoJdbc? – bvulaj Mar 05 '12 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 05 '12 at 21:12