Skip to content

Commit

Permalink
Quite some refactoring regarding auditing samples.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odrotbohm committed Jan 12, 2014
1 parent d9bbdcc commit f90c7fb
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<developers>
<developer>
<id>gierke</id>
<id>ogierke</id>
<name>Oliver Gierke</name>
<email>ogierke@gopivotal.com</email>
<url>http://www.olivergierke.de</url>
Expand All @@ -36,7 +36,7 @@
<properties>
<spring.version>4.0.0.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version>
<spring-data-jpa.version>1.5.0.M1</spring-data-jpa.version>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.auditing;
package org.springframework.data.jpa.example.repository.auditing;

import javax.persistence.Entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.auditing;
package org.springframework.data.jpa.example.repository.auditing;

import org.springframework.data.repository.CrudRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.auditing;
package org.springframework.data.jpa.example.repository.auditing;

import org.springframework.data.domain.AuditorAware;

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

private AuditableUser auditor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Package showing auditing support with Spring Data repositories.
*/
package org.springframework.data.jpa.example.auditing;
package org.springframework.data.jpa.example.repository.auditing;
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@
*/
package org.springframework.data.jpa.example.repository;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.example.domain.User;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* @author Thomas Darimont
*/
@Configuration
@EnableTransactionManagement
public class InfrastructureConfig {

@Bean
Expand All @@ -43,26 +38,24 @@ public DataSource dataSource() {
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
public JpaTransactionManager transactionManager() {
return new JpaTransactionManager();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.HSQL);
vendorAdapter.setShowSql(true);
vendorAdapter.setGenerateDdl(true);

LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
factoryBean.setPersistenceUnitName("jpa.sample");
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan(User.class.getPackage().getName());
return factoryBean;
}
factoryBean.setPackagesToScan("org.springframework.data.jpa.example");
factoryBean.setMappingResources("META-INF/orm.xml");

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.HSQL);
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setGenerateDdl(true);
return jpaVendorAdapter;
return factoryBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.data.jpa.example.auditing.AuditableUser;
import org.springframework.data.jpa.example.auditing.AuditableUserRepository;
import org.springframework.data.jpa.example.auditing.AuditorAwareImpl;
import org.springframework.data.jpa.example.repository.auditing.AuditableUser;
import org.springframework.data.jpa.example.repository.auditing.AuditableUserRepository;
import org.springframework.data.jpa.example.repository.auditing.AuditorAwareImpl;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.auditing;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.example.repository.InfrastructureConfig;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration;

/**
* Test case to show Spring Data JPA auditing with JavaConfig
*
* @author Oliver Gierke
*/
@ContextConfiguration
public class JavaConfigAuditableUserSample extends AbstractAuditableUserSample {

@Configuration
@EnableJpaRepositories
@EnableJpaAuditing
static class Config extends InfrastructureConfig {

@Bean
AuditorAwareImpl auditorAware() {
return new AuditorAwareImpl();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?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="jpa.sample"/>
<persistence-unit name="jpa.sample.plain">

<persistence-unit name="jpa.sample.plain">
<class>org.springframework.data.jpa.example.domain.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
Expand All @@ -11,4 +12,5 @@
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>

</persistence>
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
-->
<jpa:auditing auditor-aware-ref="auditorAware" />

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

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

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
<property name="database" value="HSQL" />
</bean>
</property>
<property name="persistenceUnitName" value="jpa.sample" />
<property name="packagesToScan" value="org.springframework.data.jpa.example" />
<property name="mappingResources" value="META-INF/orm.xml" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>

0 comments on commit f90c7fb

Please sign in to comment.