Skip to content

Commit 7dec42d

Browse files
committed
Added sample for Java 8 auditing.
1 parent b8143c6 commit 7dec42d

File tree

9 files changed

+264
-3
lines changed

9 files changed

+264
-3
lines changed

java8-auditing/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.data.jpa.examples</groupId>
7+
<artifactId>spring-data-jpa-examples-parent</artifactId>
8+
<version>1.1.0.RELEASE</version>
9+
</parent>
10+
11+
<artifactId>java8-auditing</artifactId>
12+
<name>Spring Data JPA - Auditing on Java 8 and Spring 4</name>
13+
14+
<properties>
15+
<spring.version>4.0.1.BUILD-SNAPSHOT</spring.version>
16+
<spring-data-jpa.version>1.5.0.BUILD-SNAPSHOT</spring-data-jpa.version>
17+
</properties>
18+
19+
<dependencies>
20+
21+
<!-- TODO: Remove, once https://github.com/spring-projects/spring-boot/pull/184 gets accepted -->
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-aspects</artifactId>
25+
<version>${spring.version}</version>
26+
</dependency>
27+
28+
</dependencies>
29+
30+
<repositories>
31+
32+
<repository>
33+
<id>spring-libs-snapshot</id>
34+
<url>http://repo.spring.io/libs-snapshot</url>
35+
</repository>
36+
37+
</repositories>
38+
39+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2013 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.examples.java8;
17+
18+
import java.time.ZonedDateTime;
19+
20+
import javax.persistence.GeneratedValue;
21+
import javax.persistence.Id;
22+
import javax.persistence.MappedSuperclass;
23+
24+
import org.springframework.data.annotation.CreatedDate;
25+
import org.springframework.data.annotation.LastModifiedDate;
26+
27+
/**
28+
* @author Oliver Gierke
29+
*/
30+
@MappedSuperclass
31+
public class AbstractEntity {
32+
33+
@Id @GeneratedValue Long id;
34+
35+
@CreatedDate//
36+
// @Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentZonedDateTime")//
37+
ZonedDateTime createdDate;
38+
39+
@LastModifiedDate//
40+
// @Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentZonedDateTime")//
41+
ZonedDateTime modifiedDate;
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2013 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.examples.java8;
17+
18+
import javax.persistence.Entity;
19+
20+
/**
21+
* @author Oliver Gierke
22+
*/
23+
@Entity
24+
public class Customer extends AbstractEntity {
25+
26+
String firstname, lastname;
27+
28+
public Customer(String firstname, String lastname) {
29+
30+
this.firstname = firstname;
31+
this.lastname = lastname;
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2013 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.examples.java8;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
20+
/**
21+
* @author Oliver Gierke
22+
*/
23+
public interface CustomerRepository extends CrudRepository<Customer, Long> {
24+
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
3+
4+
<persistence-unit-metadata>
5+
<persistence-unit-defaults>
6+
<entity-listeners>
7+
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
8+
</entity-listeners>
9+
</persistence-unit-defaults>
10+
</persistence-unit-metadata>
11+
12+
</entity-mappings>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2013 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.examples.java8;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.ComponentScan;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
30+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
31+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
32+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
33+
import org.springframework.orm.jpa.JpaTransactionManager;
34+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
35+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
36+
import org.springframework.test.context.ContextConfiguration;
37+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38+
39+
/**
40+
* Integration test to show the usage of Java 8 date time APIs with Spring Data JPA auditing.
41+
* <p>
42+
* TODO: Move to @EnableAutoConfiguration when Boot 0.5 M8 is released
43+
*
44+
* @author Oliver Gierke
45+
*/
46+
@RunWith(SpringJUnit4ClassRunner.class)
47+
@ContextConfiguration
48+
public class Java8AuditingIntegrationTests {
49+
50+
@Configuration
51+
@ComponentScan
52+
@EnableJpaRepositories
53+
@EnableJpaAuditing
54+
static class Config {
55+
56+
@Bean
57+
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
58+
59+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
60+
vendorAdapter.setGenerateDdl(true);
61+
62+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
63+
factory.setJpaVendorAdapter(vendorAdapter);
64+
factory.setPackagesToScan(getClass().getPackage().getName());
65+
factory.setMappingResources("META-INF/orm.xml");
66+
factory.setDataSource(dataSource);
67+
68+
return factory;
69+
}
70+
71+
@Bean
72+
DataSource dataSource() {
73+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
74+
}
75+
76+
@Bean
77+
JpaTransactionManager transactionManager() {
78+
return new JpaTransactionManager();
79+
}
80+
}
81+
82+
@Autowired CustomerRepository repository;
83+
84+
@Test
85+
public void auditingSetsJdk8DateTimeTypes() {
86+
87+
Customer customer = repository.save(new Customer("Dave", "Matthews"));
88+
89+
assertThat(customer.createdDate, is(notNullValue()));
90+
assertThat(customer.modifiedDate, is(notNullValue()));
91+
}
92+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="info" />
11+
<logger name="org.springframework.boot" level="debug" />
12+
<logger name="org.hibernate.tool.hbm2ddl" level="trace" />
13+
14+
<root level="error">
15+
<appender-ref ref="console" />
16+
</root>
17+
18+
</configuration>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>spring-data-jpa-example</module>
2323
<module>spring-data-jpa-showcase</module>
2424
<module>spring-data-jpa-interceptors</module>
25+
<module>java8-auditing</module>
2526
</modules>
2627

2728
<developers>

spring-data-jpa-showcase/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
<description>Sample project showing how Spring Data JPA eases implementing repositories over a plain JPA/Spring approach</description>
1616

1717
<properties>
18-
<querydsl.version>3.2.4</querydsl.version>
19-
<spring.version>4.0.0.RC1</spring.version>
18+
<querydsl.version>3.3.0</querydsl.version>
2019
</properties>
2120

2221
<build>
@@ -104,7 +103,7 @@
104103
<plugin>
105104
<groupId>com.mysema.maven</groupId>
106105
<artifactId>apt-maven-plugin</artifactId>
107-
<version>1.1.0</version>
106+
<version>1.1.1</version>
108107
<dependencies>
109108
<dependency>
110109
<groupId>com.mysema.querydsl</groupId>

0 commit comments

Comments
 (0)