This shows basic usage of Spring Data JPA for some simple queries, and create/update/delete. It has a lot of customization points, including items like query auto-generation by convention, query hints, and auditing. This example builds on the JPA Joined Inheritance example and has the same data structure.
Instead of using context:component-scan, Spring Data JPA's
jpa namespace is used. The jpa:repositories scans for all interfaces
that extend JpaRepository
and creates implementations for use at runtime.
Excerpt from PersonRepositoryTest-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="org.springbyexample.orm.repository" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/> <jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classpath:/schema.sql" encoding="UTF-8" /> </jdbc:embedded-database> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource" p:persistenceUnitName="hsql"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> </bean> ... </beans>