Pages

Monday, 31 March 2014

Lesson 08 - Spring ORM (with Hibernate)

NoteAs of Spring 3.0, Spring requires Hibernate 3.2 or later. We are using Spring 3.2.4

In this section we will be looking at spring's capabilities of supporting a ORM framework such as hibernate, JPA, JDO etc but we are concentrating on hibernate only. We get first class support for hibernate in terms of resource management, DAO implementations, tx management. We configure all supported features of OR mapping tool using dependency injection. We will take advantage of generic exception hierarchy. Following are the advantages of using spring with any other ORM framework is:

Spring IOC makes it easier to swap the implementations and configuration of hiernate's SessionFactory, JDBC Datasource and tx managers so we can test the app easily.

Spring can wrap the technology specific exceptions to generic spring exception hierarchy (which are unchecked). you never loos original exception as spring just wraps your original exception as dao specific exception.

Spring application context manages SessionFactory(hibernate)/ DataSource(jdbc)/ EntityManager(jpa) instances this makes very easy to change/manage them.Spring makes it easy to bind a hibernate Session to currently executing thread transparently by exposing the current Session through the SessionFactory.

Spring has powerful declarative, AOP style or XML style transactional management infrastructure.It allows to swap transaction manager with out affecting ORM related code.

General ORM Integration Considerations

Main goal of Spring ORM is to have clear application layering with your choice of data access(jdbc,hibernate, jpa, jdo) and transaction technology along with loosely coupled application objects. These application objects are loosely coupled and free from hard coded API specific lookups or classes/interfaces. In typical spring application, data access templates, DAOs, Tx Managers, Business Servies are merely java beans managed by spring container.

Spring provides excellent resource and tx management infrastructure.As you have seen JdbcTemplate provides connection handling and converting SQLExceptions to spring specific dao exception hierarchy i.e DataAccessException (unchecked) and when it comes to it hooks into spring tx support and support both Jdbc, JTA transactions. In the below section we see how hibernate is supported wrt to resource handling and tx management. When we use hibernate/jdo/jpa we need to have know how native (to persistent technology) exceptions. But as we have seen with the use of @Repository annotation specfic exceptions could be transalted to unchecked dao exception hierarchy of exceptions. The Bean Post Processors looks for all exception translators (i.e implementers of PersistenceExceptionTranslator interface) and all beans marked with @Repository will be intercepte on the occurence of an exception.In summary: you can implement DAOs based on the plain persistence technology's API and annotations, while still getting benefits of Spring managed Txs, DI, spring's custom exception hierarchies.

Hibernate

In this section we are going to look into hibernate as a data access technology and see how spring orm can be used. As this is not a complete hibernate material I will be covering topics close to spring hibernate orm area but not in depth details on hibernate. As you might aware, SessionFactory is the back bone of any hibernate application. Since spring IOC allows creation of beans following xml can be easily changed to another data source implementation with just configuration itself.
You must change the maven pom to use spring-orm as well as shown below:




Hibernate has a concept called "Contextual sessions" where in hibernate maintains one session per transaction which is almost same as spring's synchronisation of one session per transaction. Spring's LocalSessionFactoryBean supports Hibernate's SessionFactory.getCurrentSession() method for any Spring transaction strategy, returning the current Spring-managed transactional Session even with HibernateTransactionManager. Of course, the standard behavior of that method remains the return of the current Session associated with the ongoing JTA transaction (if any). Let us take an example and see how this works..
Let us understand the above example.. In this example, Product is a simply a bean with few properties mapped to a database table called tbl_products in .hbm.xml file. This is usual hibernate stuff. Important thing to recognise is we are passing SessionFactory as a (class member) using Spring IOC. So your code would be using only hibernate related apis only it never touches spring. We are calling openSession() method on factory to open a session and saving the Product in to the tables. In this example we have manually starting the transaction and commiting/rolling back in case of errors. But as we saw, spring's declarative transaction management does the magic like shown in below example. Here I am showing how HibernateTransactionManager debug statements are displayed, so we need to edit log4j.properties as well:
now let us see what happens if we try to use programmatic transaction management as we learned in previous chapter. The below example is written using both programmatic and declarative approach.


So this wraps the discussion on spring ORM. There are plenty of things as normally you do in hibernate but just simply get LocalSesstionFactoryBean as spring IOC to make use of all powerful spring features.



Previous Next

No comments:

Post a Comment