www.edureka.co/persistence-with-hibernate
Leverage Hibernate and Spring Features Together
View Persistence with Hibernate course details at www.edureka.co/persistence-with-hibernate
For Queries:
Post on Twitter @edurekaIN: #askEdureka
Post on Facebook /edurekaIN
For more details please contact us:
US : 1800 275 9730 (toll free)
INDIA : +91 88808 62004
Email Us : webinars@edureka.co
Slide 2 www.edureka.co/persistence-with-hibernate
At the end of this webinar, you will be able to understand:
Objectives
 What are Hibernate & Spring Frameworks
 Advantages of Using Spring and Hibernate Together
 Connection Between Hibernate and Spring Frameworks
 Spring Transaction Management Using Hibernate
 Demo
Slide 3 www.edureka.co/persistence-with-hibernate
An Object relational mapping library of java language for Object persistence
and SQL databases
Found in 2001 by Gavin King
Transparent persistence of java objects with relational database
Provides query language in synch with SQL
Open source library
Provides solutions for object relational impedance mismatch problems
Hibernate
Slide 4 www.edureka.co/persistence-with-hibernate
Provides a simple API for storing and retrieving Java objects directly to and from the database
Non-intrusive: No need to follow specific rules or design patterns
Transparent: Your object model is unaware
Persistence using Hibernate
JavaObject
int id;
String name;
String getName()
int getId()
void setName(String)
SQL Table
id [int] primary key,
name [varchar(50)],
Magic Happens Here
(O/R Mapper – i.e. Hibernate)
Slide 5Slide 5Slide 5 www.edureka.co/persistence-with-hibernate
Spring
An open source framework
Enables to build applications using POJO (Plain Old Java Objects)
Handles the complete infrastructure required for application from end to
end
Makes development of JavaEE (Java EnterPrise Applications) easier
Light weight and transparent
Use of IOC Containers
Supports ORM and Transaction Management
Slide 6Slide 6Slide 6 www.edureka.co/persistence-with-hibernate
Spring with Hibernate
An extensive support for ORM frameworks like
Hibernate , JPA
Provides complete infrastructure for any ORM layer used
in the application
 IOC of spring handles the SessionFactory instances of
hibernate
Configurations of hibernate are taken care efficiently by
spring application contexts
Use of hibernate.cfg.xml can be taken care by spring
applicationContext.xml
Declarative Transaction management
Built in support for exception handling
Slide 7Slide 7Slide 7 www.edureka.co/persistence-with-hibernate
Spring supports two different approach for transaction management:
Programmatic Transaction Management
» Transactions are managed by programming codes
» Highly flexible
» Difficult in maintenance
» Spring TransactionTemplate is used for programmatic approach
Declarative Transaction Management
» Transactions are handled declaratively by XML or Annotation
» Will have lesser impact with the code of application
» Extensive support from Spring AOP
» Most widely used
Transaction Management with Spring
Slide 8Slide 8Slide 8 www.edureka.co/persistence-with-hibernate
Spring Aspect Oriented Programming enables to declare transaction
declaratively with Aspect
Aspect scatters across methods, class and even objects
Can be used in xml and annotations
AOP declarative transaction:-
» Transaction-handling advice is created using <tx:advice/> and
pointcut is defined to make transactional advice that matches all
methods
» Advice begins transaction on before calling the method
» On successful execution of method advice commits the
transaction
» On failure advice rolls back
Spring AOP Transaction Management
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="createOperation"
expression="execution(*
com.tutorialspoint.StudentJDBCTemplate.create
(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-
ref="createOperation"/>
</aop:config>
Slide 9 www.edureka.co/persistence-with-hibernate
DEMO : Spring Transaction Management Using Hibernate
Slide 10 www.edureka.co/persistence-with-hibernate
Course class
Spring and Hibernate Example
Slide 11 www.edureka.co/persistence-with-hibernate
CourseDAO class
Spring and Hibernate Example (Contd.)
Slide 12 www.edureka.co/persistence-with-hibernate
This is the mapping file which will take care of mapping between your Courses table and Course class
course.hbm.xml
Spring and Hibernate Example (Contd.)
Slide 13 www.edureka.co/persistence-with-hibernate
applicationContext.xml (contd.)
Spring and Hibernate Example (Contd.)
Slide 14 www.edureka.co/persistence-with-hibernate
In the applicationContext.xml file we are declaring lots of bean definition
One is for DataSource definition, one is for Hibernate Session Factory with the mapping resource course.hbm.xml
Then we are creating HibernateTemplate bean setting its sessionFactory property to mysessionfactory
Last is the bean definition of CourseDAO class setting its template property to HibernateTemplate bean
Spring and Hibernate Example (Contd.)
Slide 15 www.edureka.co/persistence-with-hibernate
When you run the TestClass, you will see one row in courses table
Spring and Hibernate Example (Contd.)
Questions
Slide 16 www.edureka.co/persistence-with-hibernate
Slide 17 Course Url

Leverage Hibernate and Spring Features Together

  • 1.
    www.edureka.co/persistence-with-hibernate Leverage Hibernate andSpring Features Together View Persistence with Hibernate course details at www.edureka.co/persistence-with-hibernate For Queries: Post on Twitter @edurekaIN: #askEdureka Post on Facebook /edurekaIN For more details please contact us: US : 1800 275 9730 (toll free) INDIA : +91 88808 62004 Email Us : webinars@edureka.co
  • 2.
    Slide 2 www.edureka.co/persistence-with-hibernate Atthe end of this webinar, you will be able to understand: Objectives  What are Hibernate & Spring Frameworks  Advantages of Using Spring and Hibernate Together  Connection Between Hibernate and Spring Frameworks  Spring Transaction Management Using Hibernate  Demo
  • 3.
    Slide 3 www.edureka.co/persistence-with-hibernate AnObject relational mapping library of java language for Object persistence and SQL databases Found in 2001 by Gavin King Transparent persistence of java objects with relational database Provides query language in synch with SQL Open source library Provides solutions for object relational impedance mismatch problems Hibernate
  • 4.
    Slide 4 www.edureka.co/persistence-with-hibernate Providesa simple API for storing and retrieving Java objects directly to and from the database Non-intrusive: No need to follow specific rules or design patterns Transparent: Your object model is unaware Persistence using Hibernate JavaObject int id; String name; String getName() int getId() void setName(String) SQL Table id [int] primary key, name [varchar(50)], Magic Happens Here (O/R Mapper – i.e. Hibernate)
  • 5.
    Slide 5Slide 5Slide5 www.edureka.co/persistence-with-hibernate Spring An open source framework Enables to build applications using POJO (Plain Old Java Objects) Handles the complete infrastructure required for application from end to end Makes development of JavaEE (Java EnterPrise Applications) easier Light weight and transparent Use of IOC Containers Supports ORM and Transaction Management
  • 6.
    Slide 6Slide 6Slide6 www.edureka.co/persistence-with-hibernate Spring with Hibernate An extensive support for ORM frameworks like Hibernate , JPA Provides complete infrastructure for any ORM layer used in the application  IOC of spring handles the SessionFactory instances of hibernate Configurations of hibernate are taken care efficiently by spring application contexts Use of hibernate.cfg.xml can be taken care by spring applicationContext.xml Declarative Transaction management Built in support for exception handling
  • 7.
    Slide 7Slide 7Slide7 www.edureka.co/persistence-with-hibernate Spring supports two different approach for transaction management: Programmatic Transaction Management » Transactions are managed by programming codes » Highly flexible » Difficult in maintenance » Spring TransactionTemplate is used for programmatic approach Declarative Transaction Management » Transactions are handled declaratively by XML or Annotation » Will have lesser impact with the code of application » Extensive support from Spring AOP » Most widely used Transaction Management with Spring
  • 8.
    Slide 8Slide 8Slide8 www.edureka.co/persistence-with-hibernate Spring Aspect Oriented Programming enables to declare transaction declaratively with Aspect Aspect scatters across methods, class and even objects Can be used in xml and annotations AOP declarative transaction:- » Transaction-handling advice is created using <tx:advice/> and pointcut is defined to make transactional advice that matches all methods » Advice begins transaction on before calling the method » On successful execution of method advice commits the transaction » On failure advice rolls back Spring AOP Transaction Management <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="createOperation" expression="execution(* com.tutorialspoint.StudentJDBCTemplate.create (..))"/> <aop:advisor advice-ref="txAdvice" pointcut- ref="createOperation"/> </aop:config>
  • 9.
    Slide 9 www.edureka.co/persistence-with-hibernate DEMO: Spring Transaction Management Using Hibernate
  • 10.
  • 11.
    Slide 11 www.edureka.co/persistence-with-hibernate CourseDAOclass Spring and Hibernate Example (Contd.)
  • 12.
    Slide 12 www.edureka.co/persistence-with-hibernate Thisis the mapping file which will take care of mapping between your Courses table and Course class course.hbm.xml Spring and Hibernate Example (Contd.)
  • 13.
  • 14.
    Slide 14 www.edureka.co/persistence-with-hibernate Inthe applicationContext.xml file we are declaring lots of bean definition One is for DataSource definition, one is for Hibernate Session Factory with the mapping resource course.hbm.xml Then we are creating HibernateTemplate bean setting its sessionFactory property to mysessionfactory Last is the bean definition of CourseDAO class setting its template property to HibernateTemplate bean Spring and Hibernate Example (Contd.)
  • 15.
    Slide 15 www.edureka.co/persistence-with-hibernate Whenyou run the TestClass, you will see one row in courses table Spring and Hibernate Example (Contd.)
  • 16.
  • 17.