Greetings, Currently developing small web service application where response from web service (using CXF + Spring) processed and saved to database. To work with database I am using Hibernate(3.5). Browsing some Hibernate + Spring example on web, I often can see the usage of HibernateTemplate so I am a bit confused about this moment and wanted to ask:
Do you use HibernateTemplate in your Hibernate3 applications? When does HibernateTemplate can make your development life better and based on what points can I decide do I need to use it or not ?
Thanks.
HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception.
Spring is used to develop application from desktop to Web. Hibernate is used to access data layer and Struts is used for Web frameworks. Get to know everything about Java coding with special approach, industry expert offers training to the candidates with real-time projects.
If you are going to use Spring Data JPA (as your pom. xml would suggest) there's no sense in using obsolete HibernateTemplate . Spring Boot doesn't create SessionFactory bean when Spring Data JPA is in use, however it creates EntityManagerFactory . You may autowire it and unwrap to SessionFactory .
The Spring framework provides HibernateTemplate class, so you don't need to follow so many steps like create Configuration, BuildSessionFactory, Session, beginning and committing transaction etc. So it saves a lot of code.
All spring templates (hibernate, jdbc, rest, jpa etc.) have the same pros and cons:
Pro: They perform common setup routines for you, let you skip the boilerplate and concentrate on the logic you want.
Con: you are coupling your application tightly to the spring framework. For this reason, Spring recommends that HibernateTemplate
no longer be used.
Specifically, what HibernateTemplate
did for you was to automatically open and close sessions and commit or rollback transactions after your code executed. However, all of this can be achieved in an aspect-oriented way using Spring's Declarative Transaction Management.
Reference:
Update:
As of Spring 3.1 (and newer versions), HibernateTemplate
has been removed. See Hibernate for the currently suggested usage patterns.
Let me clarify one thing that Spring's HibernateTemplate will not be supported going forward, that means Hibernate 4+ versions do not support HibernateTemplate. So it is advised to use declarative transaction management as suggested by Sean.
HibernateTemplate encapsulates a number of things for you to make your life easier.
It's your choice to use it or not. For that matter, you can work with a database without Hibernate. Spring's JDBC stuff is very good. You might find it easier to get your problem done without having to learn Hibernate.
The OpenSessionInViewFilter pattern is effective. This opens a Hibernate session & binds it to your thread, during the processing of every request. OpenSessionInView also extends the Session and loadability to View rendering & the View layer, which decreases coupling & complexity (by enabling that to 'just work').
My philosophies don't really agree with aspect-based/ declarative transaction management. I like to make major state-change/ lifecycle events 'explicit', since they should be absolutely definite -- not weakly dependent on multiple hidden & indirect layers, which may or may not work.
It provides a point to debug at.
TX commit is only one line of code; but it's the major one you want to breakpoint on. No longer syntactically, than a 'transactional' declaration; but a hell of a lot more definite.
Frankly I find "user commands" or "requests", which are the proper place to initiate a transaction & control transactionality from, should be well-structured, well-identified & fairly explicit within the application.
(I did have trouble getting the aspect class-loading stuff to work, trying it when it first came out. My assessment is that compared to well-written OO code, aspect has only limited marginal value.)
Tip: I generally make a helper class, to make it really convenient to get the Session & to commit the Transaction.
HbHelper or somesuch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With