This component embraces of JDBC, ORM, Hibernate, JMS, OXM and translation modules. These modules provide support to deal with databases.
Spring integration with Hibernate
In Hibernate Framework, developers will provide all the database files will have below extension.
hibernate.cfg.xml file
While integrating Hibernate with Spring framework database will have below extension.
ApplicationContext.xml file
Use of Spring framework over Hibernate:
Spring framework consists of Hibernate template class, it consists of a set of methods, then the developer can easily work on the applications. Hibernate template class will provide various method to retrieve the information from database and it can collaborate with Hibernate session.
Actual code in Hibernate:
[java]Configuration c=new Configuration();
cfg.configure("hibernate.c.xml");
// session factory object
SessionFactory f=c.buildSessionFactory();
// session object
Session s=f.openSession();
// transaction object
Transaction t=s.beginTransaction();
Student s=new Student(122,"sai",20000);
session.persist(e);//persisting the object
t.commit();// commited
session.close();
[/java]
As per the above code Hibernate involves more steps, by using Hibernate Template classes in Spring code will be reduced as follows.
[java]Student s=new Student(122,"sai",20000);
hibernateTemplate.save(e);[/java]
Methods of Hibernate template class
Method |
Functionality |
Void persist(object entity) |
object will be persists by this method. |
Serializable save(object entity) |
returns id after persisting object. |
Void saveOrUpdate(Object entity) |
after finding id it updates else saves . |
Void update(object entity) |
object will be updated. |
Void delete(object entity) |
object will be deleted depends on id. |
Object get(Class entityClass,Serializable id) |
persistent object will be returned depends on id. |
Object load(Class entityClass,Serializable id) |
persistent object will be returned depends on id. |
List loadAll |
persistent object returns. |