An´alisis inicial de requisitos
5.4. Análisis y resultados del formulario
PREREQUISITES
Recipe 2.5, “Working with JPA”
KEY TECHNOLOGIES JPA, Spring Data JPA Background
Until recently, it has been up to developers to implement their own DAO framework, as we showed how to do in recipe 2.4. With Spring Data JPA, that has changed. Spring Data JPA provides an official DAO framework as part of Spring itself—one that includes several useful characteristics and features:
■ The generic DAO interface is comprehensive. It includes support for paging, sorting, and batch operations.
■ Another nice touch is that in addition to the entity class, the ID class is a type parameter. That way you can use Long, String, or any other Serializable type in a typesafe way.
■ Spring Data JPA automatically generates concrete DAO classes using dynamic proxies.
■ It automatically generates query method implementations based on the name of the query method.
■ It automatically translates exceptions to Spring’s DataAccessException hierarchy.
Once you’ve updated your app to use JPA instead of using Hibernate directly, you can take advantage of Spring Data JPA. This recipe shows how.
Problem
Replace your custom DAO framework with the simpler and more powerful Spring Data JPA.
Solution
Spring Data JPA provides a generic DAO interface called JpaRepository that serves the same function your Dao interface does, although in a more comprehensive way.
Let’s begin by creating a ContactDao interface based on JpaRepository.
package com.springinpractice.ch02.dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.springinpractice.ch02.model.Contact;
Listing 2.15 ContactDao.java, revised to use Spring Data JPA
public interface ContactDao extends JpaRepository<Contact, Long> { List<Contact> findByEmailLike(String email);
}
To support contacts, you pass the Contact and Long type parameters into JpaReposi-tory
B
. Those are the domain class and the ID class, respectively.You also declare a finder method to support email search at
C
. The method name is significant because it’s what allows Spring Data JPA to figure out how to build the corresponding query dynamically. See the Spring Data JPA reference documentation for more information on how Spring Data JPA maps method names to queries.Now let’s look at the revised beans-service.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/
➥ spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/Sip02DS"
resource-ref="true" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.
➥ LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.springinpractice.ch02.model">
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect </prop>
Listing 2.16 beans-service.xml, revised to use Spring Data JPA
Extends
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven />
<jpa:repositories base-package="com.springinpractice.ch02.dao" />
<context:component-scan
base-package="com.springinpractice.ch02.service.impl" />
</beans>
We’ve bolded the few parts that have changed from listing 2.14. You add the jpa namespace (and its schema location) to the configuration
B
. And you replace the DAO component scan with a <jpa:repositories> definitionC
, which is almost like a component scan, except that it discovers the DAO interfaces and then automatically provides dynamic proxy implementations for you. That’s right: you don’t have to write the DAO implementations. Nor do you need to mark up the interface with @Repository or@Component, because <jpa:repositories> can determine which interfaces imple-ment JpaRepository.
Your data access tier has come a long way, when you think back to recipe 2.1. What started out as a bunch of SQL with named parameter substitutions and row mappings is now effectively a single findByEmailLike() method declaration.
Run the app to confirm that it functions as expected with the new Spring Data JPA data access tier in place.
Discussion
This recipe provides only a flavor of what Spring Data JPA is all about. But even with this small taste, it’s clear that Spring Data JPA provides a powerful and streamlined approach to building out a data access tier. For more information on Spring Data JPA, visit the project home page at www.springsource.org/spring-data/jpa.
2.7 Summary
In this chapter, you learned how to put together a persistence and transaction infra-structure using a DataSource, Hibernate, the data access object design pattern, and declarative transactions. This popular combination makes for a powerful back end because it cleanly separates the infrastructure from the domain logic.
We also explored building DAOs against JPA rather than coding directly against Hibernate. Even though this book uses Hibernate directly, it’s useful to see how theJPA-based approach works and how Spring makes it simple to move from one to the other.
Finally, we took a brief glimpse at Spring Data JPA, one of the projects under the Spring Data umbrella. Spring Data JPA simplifies the development of JPA-based DAOs.
Discovers DAOs
C
In addition to JPA support, Spring Data has projects for several nonrelational data stores, including Hadoop, GemFire, Redis, Riak, MongoDB, Neo4j, and Amazon S3.
In chapter 3, we’ll move up the stack to the web tier, where you’ll see how to develop web-based model-view-controller (MVC) apps using Spring Web MVC.
65