Adds an example demonstrating the use of Spring Data Envers.
Closes #603 Original pull request: #604.
This commit is contained in:
committed by
Mark Paluch
parent
66a78013a3
commit
fef817eb22
8
jpa/envers/README.adoc
Normal file
8
jpa/envers/README.adoc
Normal file
@@ -0,0 +1,8 @@
|
||||
# Spring Data JPA - Spring Data Envers
|
||||
|
||||
This project contains an example of how to use Spring Data JPA in combination with Spring Data Envers.
|
||||
|
||||
Envers module is a Hibernate module that works both with Hibernate and JPA. It aims to provide an easy auditing / versioning solution for entity classes.
|
||||
|
||||
Spring Data Envers integrates it with Spring Data JPA and makes revision information available directly from JPA based repositories.
|
||||
|
||||
20
jpa/envers/pom.xml
Normal file
20
jpa/envers/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-data-jpa-examples</artifactId>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<version>2.0.0.BUILD-gh-603-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-jpa-envers</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-envers</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
*
|
||||
* https://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 example.springdata.jpa.envers;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
/**
|
||||
* Configuration for the Envers Demo.
|
||||
* <p>
|
||||
* Note that a special {@literal repositoryFactoryBeanClass} is configured for Spring
|
||||
* Data Envers support.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
|
||||
public class EnversDemoConfiguration {}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
*
|
||||
* https://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 example.springdata.jpa.envers;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
/**
|
||||
* An entity with a single non-technical attribute
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Entity
|
||||
@Audited
|
||||
public class Person {
|
||||
|
||||
@Id @GeneratedValue Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@Version Long version;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
*
|
||||
* https://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 example.springdata.jpa.envers;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
|
||||
/**
|
||||
* Repository for persons with revision support
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public interface PersonRepository extends CrudRepository<Person, Long>, RevisionRepository<Person, Long, Long> {
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
*
|
||||
* https://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 example.springdata.jpa.envers;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.history.Revision;
|
||||
import org.springframework.data.history.RevisionMetadata.RevisionType;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Demonstrating the usage of Spring Data Envers
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class EnversIntegrationTests {
|
||||
|
||||
@Autowired PersonRepository repository;
|
||||
@Autowired TransactionTemplate tx;
|
||||
|
||||
@Test
|
||||
void testRepository() {
|
||||
|
||||
Person updated = preparePersonHistory();
|
||||
|
||||
Revisions<Long, Person> revisions = repository.findRevisions(updated.id);
|
||||
|
||||
|
||||
Iterator<Revision<Long, Person>> revisionIterator = revisions.iterator();
|
||||
|
||||
checkNextRevision(revisionIterator, "John", RevisionType.INSERT);
|
||||
checkNextRevision(revisionIterator, "Jonny", RevisionType.UPDATE);
|
||||
checkNextRevision(revisionIterator, null, RevisionType.DELETE);
|
||||
assertThat(revisionIterator.hasNext()).isFalse();
|
||||
|
||||
}
|
||||
|
||||
private void checkNextRevision(Iterator<Revision<Long, Person>> revisionIterator, String name, RevisionType revisionType) {
|
||||
|
||||
assertThat(revisionIterator.hasNext()).isTrue();
|
||||
Revision<Long, Person> revision = revisionIterator.next();
|
||||
assertThat(revision.getEntity().name).isEqualTo(name);
|
||||
assertThat(revision.getMetadata().getRevisionType()).isEqualTo(revisionType);
|
||||
}
|
||||
|
||||
private Person preparePersonHistory() {
|
||||
|
||||
Person john = new Person();
|
||||
john.setName("John");
|
||||
|
||||
//create
|
||||
Person saved = tx.execute(__ -> repository.save(john));
|
||||
assertThat(saved).isNotNull();
|
||||
|
||||
saved.setName("Jonny");
|
||||
|
||||
// update
|
||||
Person updated = tx.execute(__ -> repository.save(saved));
|
||||
assertThat(updated).isNotNull();
|
||||
|
||||
// delete
|
||||
tx.executeWithoutResult(__->repository.delete(updated));
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@
|
||||
<module>query-by-example</module>
|
||||
<module>vavr</module>
|
||||
<module>deferred</module>
|
||||
<module>envers</module>
|
||||
</modules>
|
||||
|
||||
<profiles>
|
||||
|
||||
Reference in New Issue
Block a user