Skip to content

Commit f90c7fb

Browse files
committed
Quite some refactoring regarding auditing samples.
Moved auditing samples into repository subpackage. Moved persistence.xml into test folder to reflect, that it's not needed by default. Changed LCEMFB configuration to include META-INF/orm.xml (needed for the auditing example). Moved XML configuration files into src/test/resources. Upgraded to Spring Data JPA 1.5 M1 to be able to use @EnableJpaAuditing.
1 parent d9bbdcc commit f90c7fb

File tree

15 files changed

+77
-37
lines changed

15 files changed

+77
-37
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<developers>
2828
<developer>
29-
<id>gierke</id>
29+
<id>ogierke</id>
3030
<name>Oliver Gierke</name>
3131
<email>ogierke@gopivotal.com</email>
3232
<url>http://www.olivergierke.de</url>
@@ -36,7 +36,7 @@
3636
<properties>
3737
<spring.version>4.0.0.RELEASE</spring.version>
3838
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39-
<spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version>
39+
<spring-data-jpa.version>1.5.0.M1</spring-data-jpa.version>
4040
</properties>
4141

4242
<build>

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/auditing/AuditableUser.java renamed to spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/auditing/AuditableUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.example.auditing;
16+
package org.springframework.data.jpa.example.repository.auditing;
1717

1818
import javax.persistence.Entity;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.example.auditing;
16+
package org.springframework.data.jpa.example.repository.auditing;
1717

1818
import org.springframework.data.repository.CrudRepository;
1919

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/auditing/AuditorAwareImpl.java renamed to spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/auditing/AuditorAwareImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.example.auditing;
16+
package org.springframework.data.jpa.example.repository.auditing;
1717

1818
import org.springframework.data.domain.AuditorAware;
1919

2020
/**
2121
* Dummy implementation of {@link AuditorAware}. It will return the configured {@link AuditableUser} as auditor on every
2222
* call to {@link #getCurrentAuditor()}. Normally you would access the applications security subsystem to return the
2323
* current user.
24+
* <p>
25+
* TODO: change back to {@code AuditorAware<AuditableUser>} after SD Commons 1.7 RC1 as generic autowiring with Spring 4
26+
* will work with that version again.
2427
*
2528
* @author Oliver Gierke
2629
* @author Thomas Darimont
2730
*/
28-
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {
31+
public class AuditorAwareImpl implements AuditorAware<Object> {
2932

3033
private AuditableUser auditor;
3134

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Package showing auditing support with Spring Data repositories.
33
*/
4-
package org.springframework.data.jpa.example.auditing;
4+
package org.springframework.data.jpa.example.repository.auditing;

spring-data-jpa-example/src/test/java/org/springframework/data/jpa/example/repository/InfrastructureConfig.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@
1515
*/
1616
package org.springframework.data.jpa.example.repository;
1717

18-
import javax.persistence.EntityManagerFactory;
1918
import javax.sql.DataSource;
2019

2120
import org.springframework.context.annotation.Bean;
2221
import org.springframework.context.annotation.Configuration;
23-
import org.springframework.data.jpa.example.domain.User;
2422
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2523
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2624
import org.springframework.orm.jpa.JpaTransactionManager;
27-
import org.springframework.orm.jpa.JpaVendorAdapter;
2825
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
2926
import org.springframework.orm.jpa.vendor.Database;
3027
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
31-
import org.springframework.transaction.annotation.EnableTransactionManagement;
3228

3329
/**
3430
* @author Thomas Darimont
3531
*/
3632
@Configuration
37-
@EnableTransactionManagement
3833
public class InfrastructureConfig {
3934

4035
@Bean
@@ -43,26 +38,24 @@ public DataSource dataSource() {
4338
}
4439

4540
@Bean
46-
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
47-
return new JpaTransactionManager(emf);
41+
public JpaTransactionManager transactionManager() {
42+
return new JpaTransactionManager();
4843
}
4944

5045
@Bean
5146
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
47+
48+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
49+
vendorAdapter.setDatabase(Database.HSQL);
50+
vendorAdapter.setShowSql(true);
51+
vendorAdapter.setGenerateDdl(true);
52+
5253
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
53-
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
54-
factoryBean.setPersistenceUnitName("jpa.sample");
54+
factoryBean.setJpaVendorAdapter(vendorAdapter);
5555
factoryBean.setDataSource(dataSource());
56-
factoryBean.setPackagesToScan(User.class.getPackage().getName());
57-
return factoryBean;
58-
}
56+
factoryBean.setPackagesToScan("org.springframework.data.jpa.example");
57+
factoryBean.setMappingResources("META-INF/orm.xml");
5958

60-
@Bean
61-
public JpaVendorAdapter jpaVendorAdapter() {
62-
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
63-
jpaVendorAdapter.setDatabase(Database.HSQL);
64-
jpaVendorAdapter.setShowSql(true);
65-
jpaVendorAdapter.setGenerateDdl(true);
66-
return jpaVendorAdapter;
59+
return factoryBean;
6760
}
6861
}

spring-data-jpa-example/src/test/java/org/springframework/data/jpa/example/repository/auditing/AbstractAuditableUserSample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import org.junit.runner.RunWith;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
25-
import org.springframework.data.jpa.example.auditing.AuditableUser;
26-
import org.springframework.data.jpa.example.auditing.AuditableUserRepository;
27-
import org.springframework.data.jpa.example.auditing.AuditorAwareImpl;
25+
import org.springframework.data.jpa.example.repository.auditing.AuditableUser;
26+
import org.springframework.data.jpa.example.repository.auditing.AuditableUserRepository;
27+
import org.springframework.data.jpa.example.repository.auditing.AuditorAwareImpl;
2828
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2929
import org.springframework.test.util.ReflectionTestUtils;
3030
import org.springframework.transaction.annotation.Transactional;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.auditing;
17+
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.data.jpa.example.repository.InfrastructureConfig;
21+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
22+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
23+
import org.springframework.test.context.ContextConfiguration;
24+
25+
/**
26+
* Test case to show Spring Data JPA auditing with JavaConfig
27+
*
28+
* @author Oliver Gierke
29+
*/
30+
@ContextConfiguration
31+
public class JavaConfigAuditableUserSample extends AbstractAuditableUserSample {
32+
33+
@Configuration
34+
@EnableJpaRepositories
35+
@EnableJpaAuditing
36+
static class Config extends InfrastructureConfig {
37+
38+
@Bean
39+
AuditorAwareImpl auditorAware() {
40+
return new AuditorAwareImpl();
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<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">
3-
<persistence-unit name="jpa.sample"/>
4-
<persistence-unit name="jpa.sample.plain">
3+
4+
<persistence-unit name="jpa.sample.plain">
5+
<class>org.springframework.data.jpa.example.domain.User</class>
56
<properties>
67
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
78
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
@@ -11,4 +12,5 @@
1112
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
1213
</properties>
1314
</persistence-unit>
15+
1416
</persistence>

spring-data-jpa-example/src/main/resources/auditing-repository-context.xml renamed to spring-data-jpa-example/src/test/resources/auditing-repository-context.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
-->
1616
<jpa:auditing auditor-aware-ref="auditorAware" />
1717

18-
<bean id="auditorAware" class="org.springframework.data.jpa.example.auditing.AuditorAwareImpl" />
18+
<bean id="auditorAware" class="org.springframework.data.jpa.example.repository.auditing.AuditorAwareImpl" />
1919

20-
<jpa:repositories base-package="org.springframework.data.jpa.example.auditing" />
20+
<jpa:repositories base-package="org.springframework.data.jpa.example.repository.auditing" />
2121

2222
</beans>

spring-data-jpa-example/src/main/resources/infrastructure.xml renamed to spring-data-jpa-example/src/test/resources/infrastructure.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
<property name="database" value="HSQL" />
1616
</bean>
1717
</property>
18-
<property name="persistenceUnitName" value="jpa.sample" />
18+
<property name="packagesToScan" value="org.springframework.data.jpa.example" />
19+
<property name="mappingResources" value="META-INF/orm.xml" />
1920
</bean>
2021

21-
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
22-
<property name="entityManagerFactory" ref="entityManagerFactory" />
23-
</bean>
22+
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
2423

2524
</beans>

0 commit comments

Comments
 (0)