Accessing Database with Spring ORM and Transactions Implementing Patterns

In Chapter 7, Accessing Database with Spring and JDBC Template Patterns, we have learned how to access database using JBDC and how Spring can remove boilerplate code from the developer end to the framework by using template pattern and callbacks. In this chapter, we will learn one advanced step of accessing database using the Object Relational Mapping (ORM) Framework and managing transactions across the application.

When my son, Arnav, was one and a half years old, he used to play with a dummy mobile phone. But as he grew up, his needs too outgrew dummy mobiles to smartphones.

Similarly, when your application has a small set of data for a business tier, then JDBC works fine, but as your application grows and becomes more complex, it becomes difficult to map tables to the objects in the application. JDBC is the dummy small phone of the data access world. But with complex applications, we need Object Relational Mapping solutions that are able to map object properties to database columns. We also need more sophisticated platforms for our application at the data access layer, which create the queries and statements independently from the database technologies for us, and which we can define declaratively or programmatically.

Many ORM Frameworks are available to provide services at the data access layer of an application. Examples of such services include object relational mapping, lazy loading of data, eager loading of data, cascading, and so on. These ORM services save you from writing a lot of code for error handling, and managing resources in the application. The ORM Frameworks decrease the development time, and help to write error-free code, so that you just focus on the business requirements only. Spring doesn't implement its own ORM solution, but it provides support for many persistence frameworks such as Hibernate, the Java Persistence API (JPA), iBATIS, and Java Data Objects (JDO). Spring also provides integration points to the ORM Frameworks so that we can easily integrate the ORM Framework in our Spring application.

Spring provides support for all these technologies in your application. In this chapter, we will explore Spring's support for ORM solutions, and cover the following topics:

  • ORM Framework and used patterns
    • The data access object pattern
    • Creating DAOs using the Factory design pattern in Spring
    • The Data Mapper pattern
    • The domain model pattern
    • Proxy for the lazy loading pattern
    • The Hibernate template pattern
  • Integrating Hibernate with Spring
    • Configuring Hibernate's SessionFactory in a Spring container
    • Implementing DAOs based on plain Hibernate API
    • Transaction management strategies in Spring
    • Declarative transaction implementation and demarcation
    • Programmatic transaction implementation and demarcation
    • Best practices for Spring ORM and transaction modules in the application

Before we go on to discuss more about the ORM Frameworks, let's first look at some design patterns used in the data access layer (DAL) of the application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset