|
SYS-CON Magazines
|
Top Linux Links You Must Click On
Feature Java Feature — Using the Java Persistence API (JPA) with Spring 2.0
How to use JPA in new or existing Spring applications to achieve standardized persistence
By: Mike Keith; Rod Johnson
Apr. 30, 2007 02:00 PM
Using Spring, the transaction can be started and committed (or rolled back) at method entry and exit. All that needs to be done to achieve this is to declaratively state that the automatic transaction demarcation should happen. In Spring 2.0 the easiest way to do this is by annotating the bean class or method with the @Transactional annotation, although it's also possible to use XML metadata that doesn't require annotating Java code. The type of transaction that's started depends on the type of transaction manager that's configured in the Spring application context; knowledge of the underlying transaction infrastructure is completely abstracted from the Java code. An example of a Spring bean that's transactional and uses an entity manager to perform JPA operations is the BookInventorySystem class shown below.
package org.bookguru; This class looks fairly ordinary except that the presence of two additional annotations, @Transactional and @PersistenceContext, provides us with a great deal more functionality. The @Transactional annotation causes all the methods in the class to get an automatic transaction, so a transaction will be provided whenever a caller invokes the addBook() method. We could just as easily have annotated the method directly to get this behavior, but the likelihood of adding more methods that also need a transaction is quite high, so the class is the best place for it. The em field will get injected with an instance of EntityManager. The entity manager injected will be configured according to the named persistence unit referred to in the unitName attribute of the @PersistenceContext annotation. Named persistence units are defined and configured in the JPA persistence.xml configuration file and in the Spring application context as part of the entity manager factory bean (see Configuring the Application Context later in this article). Despite the sparseness of the operations (we could fill it in with more operations and queries, but it's sufficient for purposes of illustration), we have a functional system, and we can now turn to the configuration.
Configuring persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> The type of transaction also depends on the deployment environment. In this example, we're running in a simple Java SE virtual machine (VM) and don't have access to a JTA transaction manager, so we set the transaction type to RESOURCE_LOCAL.
Configuring the Application Context
Configuring the Entity Manager Factory Bean Configuring the entity manager factory bean involves configuring three main dependencies: the persistence unit name, the data source, and the load time weaver. This is done as follows:
<bean id="entityManagerFactory" The type of component created by this bean definition is EntityManagerFactory, which is the starting point for JPA usage. Reader Feedback: Page 1 of 1
Subscribe to our RSS feeds now and receive the next article instantly!
Subscribe to the World's Most Powerful Newsletters
|
||||||||||||||||