III. Spring ORM
Spring provides API to easily integrate spring with ORM frameworks
Popular ORM Tools are
o Hibernate
o Java Data Objects (JDO)
o JPA(Java Persistence API),
o JDO(Java Data Objects),
o iBATIS.
Standalone Hibernate DB Operations
Normal DML Hibernate blocks
Session s = sf.openSession();
Transaction tx = sf.beginTransaction();
sf.save(studentob)
tx.close();
s.close();
Normal DDL Hibernate blocks
Session s = sf.openSession();
Student ob = sf.get(student.clss, id)
s.close();
Spring with Hibernate
In spring we have HibernateTemplate class for hibernate functionality
· save()/ persist()
· update()/ merge()
· delete()
· get / load
· find(HQL) : List<T>
· findbyCriteria() : List<T>
· In this we don’t worry about close related things in spring hibernate template
· whenever u call hibernateTemplate using spring, our spring communicates with Hibernate xml and asks for connection related things
· the reduced code as ht.save(), ht.update(), ht.update()
Example:
Hibernate code
// configuration
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Student st=new Student(10,"Vijay",”HYDERABAD”);
//save the object
session.persist(st);
//transaction is commit
t.commit();
session.close();
Spring Code
Student st=new Student(10,"Vijay",”HYDERABAD”);
hibernatetmp.save(st);