Spring Series, part 2nd: When Hibernate meets Spring

Source: Internet
Author: User
Tags aop require

Naveen Balani continues his Spring series, introducing the knowledge of integrating Hibernate transactions with Spring aspect-oriented programming (AOP). The result is a durable framework that can be relied upon.

In the previous issue of this series, I introduced the Spring Framework's 7 modules, including the spring AOP and Control reversal (IOC) container. I then used a simple example to demonstrate how the IOC model, implemented by the Spring IOC container, integrates decentralized systems in a loosely coupled manner.

Now, I start with the last place I ended, with an example similar to the last one demonstrating the declarative transactions of spring AOP and spring Hibernate persistence support, so I start with an in-depth study of these two technologies.

Download the source code for this article. See Resources to access the Spring framework and Apache Ant, and the sample application running this article needs them.

Spring AOP

Software systems are usually composed of multiple components, each of which is responsible for a particular functional area. However, these components also often assume additional responsibilities beyond their core capabilities. System services, such as logging, transaction management, and security, often find themselves in the realm of other components, and the core responsibilities of these components are other things. The result is the so-called "code entanglement" or, more simply, "a mess." Aspect-oriented programming is a programming technique that attempts to solve this problem by elevating the separation of concerns to the core programming concepts.

When using AOP, you still define the public functions of the system in one place, but you can declaratively define how and where to apply this functionality. If you modularize crosscutting concerns, such as logging and transaction management, you can add new attributes to your code without having to modify each individual class. This kind of modular focus is called an aspect.

Take an enterprise application as an example. Such applications typically require services that are similar to security and transaction support. Obviously, the support for these services can be written directly into each of the classes that require service, but it is more desirable to have to write the same transaction code for a large number of transactional contexts. If you use Spring AOP for transactions, you can declaratively schedule the appropriate method calls without having to schedule them individually.

Spring AOP provides several aspects that can be used to declare transactions for JavaBean. For example, Transactionproxyfactorybean is a convenient proxy class that can intercept method calls to existing classes and apply transaction contexts to transaction beans. The actual application of this class is seen in the following example.

Hibernate

The Spring Framework provides integrated support for Hibernate, JDO, and IBATIS SQL Maps. Spring's support for Hibernate is the first level, integrating many of the IOC's convenience features and solving many of the typical Hibernate integration issues. The framework's support for Hibernate conforms to Spring's common transaction and data Access Object (DAO) exception hierarchy.

Spring provides support for creating data access applications with the selected OR mapping layer. Because everything is designed to be a reusable set of JavaBean, you can access most Spring's OR mapping support in a library format, regardless of the technology you choose. The benefit of an or mapping within the ApplicationContext or beanfactory is that it simplifies configuration and deployment.

Hibernate is a fully functional, open source or mapping framework on the Java platform. Hibernate supports the development of persistent classes that conform to the conventional Java philosophy-including associations, inheritance, polymorphism, compositing, and the Java Collection framework. The Hibernate Query Language (HQL) is designed as a miniature object-oriented extension of SQL, a bridge between objects and the world of relationships. Hibernate also supports expressing queries with primitive SQL or java-based criteria and sample queries. Hibernate uses XML (*.hbm.xml) files to map Java classes to tables, mapping JavaBean properties to database tables.

All SQL database management systems are supported through JDBC technology. Hibernate is well integrated with all popular Java EE application servers and Web containers.

actual example

A Bank application example lets you see how well Spring AOP and Hibernate work together. The bank account use case allows a user (customer) to open one or more bank accounts in one transaction. Users can request multiple bank accounts, which can be either a checking account type or a savings account type.

The application database (cloudscape™) accommodates all customer and account information. In this example, suppose there is a 1:n association between the Customer and the account class. In a real-life scenario, an association might need to be modeled by M:N in order to support a federated account.

Since users must be able to request multiple accounts in a single transaction, the first step is to implement a DOA pattern for database interaction. You then set the Spring AOP Transactionproxyfactorybean to intercept the method call and declaratively apply the transaction context to DOA.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.