Hibernate and Spring Integration

We can essentially coordinate hibernate application with spring application.

In rest system, we give all the database data hibernate.cfg.xml file.

But in the event that we will coordinate the rest application with spring, we don't have to make the hibernate.cfg.xml record. We can give all the data in the applicationContext.xml file.


Advantage of Spring system with hibernate

The Spring system gives HibernateTemplate class, so you don't have to pursue such a significant number of steps like make Configuration, BuildSessionFactory, Session, starting and submitting exchange etc.

So it spares a great deal of code.

Understanding issue without utilizing spring:

Let's comprehend it by the code of sleep given below:

Advantage of Spring structure with hibernate

The Spring structure gives HibernateTemplate class, so you don't have to pursue such huge numbers of steps like make Configuration, BuildSessionFactory, Session, starting and submitting exchange etc.

So it spares a great deal of code.

Understanding issue without utilizing spring:

Let's comprehend it by the code of sleep given below:

  1. //creating configuration  
  2. Configuration cfg=new Configuration();    
  3. cfg.configure("hibernate.cfg.xml");    
  4.     
  5. //creating seession factory object    
  6. SessionFactory factory=cfg.buildSessionFactory();    
  7.     
  8. //creating session object    
  9. Session session=factory.openSession();    
  10.     
  11. //creating transaction object    
  12. Transaction t=session.beginTransaction();    
  13.         
  14. Employee e1=new Employee(111,"arun",40000);    
  15. session.persist(e1);//persisting the object    
  16.     
  17. t.commit();//transaction is commited    
  18. session.close();    

As you can find in the code of sole rest, you need to pursue such a large number of steps.

Solution by utilizing HibernateTemplate class of Spring Framework:

Now, you don't have to pursue such a large number of steps. You can essentially compose this:

  1. Employee e1=new Employee(111,"arun",40000);    
  2. hibernateTemplate.save(e1);   hibernateTemplate.save(e1);

Methods of HibernateTemplate class

Let's see a rundown of regularly utilized strategies for HibernateTemplate class.

No.MethodDescription
1) void persist(Object substance) persists the given object.
2) Serializable save(Object substance) persists the given item and returns id.
3) void saveOrUpdate(Object element) persists or updates the given item. In the event that id is discovered, it refreshes the record generally spares the record.
4) void update(Object element) updates the given object.
5) void delete(Object element) deletes the given article based on id.
6) Object get(Class entityClass, Serializable id) returns the tenacious article based on given id.
7) Object load(Class entityClass, Serializable id) returns the steady article based on given id.
8) List loadAll(Class entityClass) returns the all the determined items.





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition