Testing Spring Data JPA
What I like about Spring libraries is that they just work, and they don't suddenly stop working. The APIs are usually well thought-out.
Spring Data JPA continues on the same track.
I've been going through recent developments in ORM / data access technology, and porting my tiny skeleton application to each. Today it's Spring Data JPA's turn.
You provide an interface like this:
#!java
package com.gueck.spring3;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ExampleDao extends CrudRepository<Example, Long> {
List<Example> findByNameLike(final String nameLike);
}And configuration like that:
#!xml
<jpa:repositories base-package="com.gueck.spring3" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="persistenceUnitName" value="default" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<beans profile="production">
<bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource" destroy-method="close">
<property name="serverName" value="${db.serverName}"/>
<property name="databaseName" value="${db.databaseName}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
</bean>
</beans>
<beans profile="dev">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:com/gueck/spring3/dev-schema.sql"/>
<jdbc:script location="classpath:com/gueck/spring3/dev-test-data.sql"/>
</jdbc:embedded-database>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2" />
</bean>
</beans>.
#!xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="default" />
</persistence>After which Spring automatically builds, generates and injects a concrete implementation of that interface to your application, where you can use it like so:
#!java
@Autowired private ExampleDao dao;
...
model.put("examples", dao.findAll());
model.put("comedies", dao.findByNameLike("%y%"));Calling the findByNameLike method results in this SQL query being run:
#!sql
select example0_.id as id0_, example0_.name as name0_ from examples example0_ where example0_.name like ?Compared to many other ways of building simple database access I've tried and used, Spring Data JPA takes the cake.
The only complaint I have with it is that when integrating JPA entities with Scaml, you have to refer to their property getters like so example.getName instead of Scaml autodetecting bean properties by simply example.name, but that's more of an issue with Scaml than Spring.