General J2EE: Spring and hibernate problem

  1. Spring and hibernate problem (3 messages)

    Hi, I am working on a project using spring and hibernate on server side.We are using IOC and ORM features of spring to integrate it with Hibernate. My question is whats the need of using spring. We can directly call java program using a servlet which accepts web requests and then we can start hibernate session which will take care of our transactions. So why actually we need spring on server side.Only for IOC?? Thanks in advance for help.

    Threaded Messages (3)

  2. Re: Spring and hibernate problem[ Go to top ]

    Hi Rohan, There are many advantages over using Spring with Hibernate or any other ORM tool. One important aspect is, you can give transactional handling task to Spring framework. And it will take care of your transactional needs, class level, method level etc... and also according to the levels defined by you. It can have interceptors, which lets us handling exceptions without having any burden on SQLException and other run time exceptions. The dependency injection lets us change codes easily with minimal effect to the code base. There are many other features available in Spring such as property file reading, data source defining and etc... Better follow this link as a start http://demo.raibledesigns.com/myusers/ Asiri
  3. Re: Spring and hibernate problem[ Go to top ]

    Thanks for the reply. But the spring also uses the hibernate transaction manager. So why cant we directly use hibernate transaction by beginning and committing the transaction. Why we want to give it to spring?
  4. Re: Spring and hibernate problem[ Go to top ]

    Hi, To my understanding Spring Framework doesn't reinvent anything new except SpringMVC(which is also very much similar to Struts1 with IOC capabilities). Spring makes the developers life easier by giving lot of helper/utility classes to use the existing technologies in better way. You can directly use Hibernate with out using Spring. But for each method in your DAO you need to get the Session object from SessionFactory and start transaction(if necessary) , execute the actual operation(save(object), saveOrUpdate(Object) and commit the transaction and finally close the Session object. But if you use Hibernate with Spring , you can extend your Dao from HibernateDaoSupport class which contains HibernateTemplate(An utility to use Hibernate Session in easy way) reference. Then you can directly perform the actual operation(saving/updating objects) by using HibernateTemplate. You don't need to do all the ground work such as getting Session object, starting Transaction, committing the transaction, closing the session. See the following example how the code will be very clear when using Hiberante with Spring than directly using Hibernate. Directly using Hibernate: public class MyDao { private SessionFactory sf = HibernateUtility.getSessionFactory(); public void saveCustomer(Customer customer) { Session ses = this.sf.openSession(); Transaction tx = ses.beginTransaction(); ses.save(customer); tx.commit(); ses.close(); } } Hibernate with Spring: public class MyDao extends HibernateDaoSupport { public void saveCustomer(Customer customer) { this.getHibernateTemplate.save(customer); } } Coming to TransactionManagement, Spring provide the Declerative Transaction Managementas well as Programmatic Transaction Management. By using Spring, you can configure the transactional attributes(at package level, class level, method level) in XML using AOP. Then you can implement the business logic with all the required transaction management support with out writing a single line of code to handle transactions. The Spring framework really makes the developers life very much easy. Thanks, K. Siva Prasad Reddy.