diff --git a/pom.xml b/pom.xml index cf869060..9ae0143d 100755 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.springframework.data spring-data-envers - 2.5.0-SNAPSHOT + 2.5.0.gh-61-SNAPSHOT org.springframework.data.build @@ -35,6 +35,12 @@ michael.igler@forward-tech.de Freelancer + + Jens Schauder + jschauder@vmware.com + VMware, Inc. + www.spring.io + diff --git a/src/main/asciidoc/envers.adoc b/src/main/asciidoc/envers.adoc new file mode 100644 index 00000000..e1e4f9da --- /dev/null +++ b/src/main/asciidoc/envers.adoc @@ -0,0 +1,198 @@ +[[envers.what]] +== What is Spring Data Envers + +Spring Data Envers differs from other Spring Data modules in that it is always used in combination with another Spring Data Module: Spring Data JPA. +It makes typical Envers queries available in repositories for Spring Data JPA. + +== What is Envers? + +Envers is a Hibernate module which adds auditing capabilities to JPA entities. +This documentation assumes you are familiar with Envers just as Spring Data Envers relies on Envers being properly configured. + +[[envers.configuration]] +== Configuration + +As a starting point for using Spring Data Envers you need a project with Spring Data JPA on the classpath and an additional `spring-data-envers` dependency. + +[source,xml,subs="+attributes"] +---- + + + + + + org.springframework.data + spring-data-envers + {version} + + + +---- + +This will also bring `hibernate-envers` into the project as a transient dependency. + +In order to enable Spring Data Envers and Spring Data JPA we need to configure two beans and a special `repositoryFactoryBeanClass` + +==== +[source,java] +---- +@Configuration +@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class) // <1> +@EnableTransactionManagement +public class EnversDemoConfiguration { +@Bean +public DataSource dataSource() { + + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + return builder.setType(EmbeddedDatabaseType.HSQL).build(); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + vendorAdapter.setGenerateDdl(true); + + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setJpaVendorAdapter(vendorAdapter); + factory.setPackagesToScan("example.springdata.jpa.envers"); + factory.setDataSource(dataSource()); + return factory; + } + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + + JpaTransactionManager txManager = new JpaTransactionManager(); + txManager.setEntityManagerFactory(entityManagerFactory); + return txManager; + } +} +---- +<1> This is the only difference to a normal Spring Data JPA configuration. `EnversRevisionRepositoryFactoryBean` ensures implementations of the methods in `RevisionRepository` are available. +==== + +In order to actually use Spring Data Envers make one or more repositories into {spring-data-commons-javadoc-base}/org/springframework/data/repository/history/RevisionRepository.html[`RevisionRepository`] by adding it as an extended interface. + +==== +[source,java] +---- +public interface PersonRepository + extends CrudRepository, + RevisionRepository // <1> +{} +---- +<1> The first type parameter `Person` denotes the entity type, the second (`Long`) the type of the id property and the last one (`Long`) is the type of the revision number. +For Envers in default configuration this should be `Integer` or `Long`. +==== + +The entity for that repository must be an entity with Envers auditing enabled, i.e. it has an `@Audited` annotation. + +[source,java] +---- +@Entity +@Audited +public class Person { + + @Id @GeneratedValue + Long id; + String name; + @Version Long version; +} +---- + +[[envers.usage]] +== Usage + +You may now use the methods from `RevisionRepository` to query the revisions of the entity as demonstrated in the following test case. + +==== +[source,java] +---- +@ExtendWith(SpringExtension.class) +@Import(EnversDemoConfiguration.class) // <1> +public class EnversIntegrationTests { + + final PersonRepository repository; + final TransactionTemplate tx; + + EnversIntegrationTests(@Autowired PersonRepository repository, @Autowired PlatformTransactionManager tm) { + this.repository = repository; + this.tx = new TransactionTemplate(tm); + } + + @Test + void testRepository() { + + Person updated = preparePersonHistory(); + + Revisions revisions = repository.findRevisions(updated.id); + + Iterator> revisionIterator = revisions.iterator(); + + checkNextRevision(revisionIterator, "John", RevisionType.INSERT); + checkNextRevision(revisionIterator, "Jonny", RevisionType.UPDATE); + checkNextRevision(revisionIterator, null, RevisionType.DELETE); + assertThat(revisionIterator.hasNext()).isFalse(); + + } + + /** + * Checks that the next element in the iterator is a Revision entry referencing a Person + * with the given name after whatever change brought that Revision into existence. + *

+ * As a side effect the Iterator gets advanced by one element. + * + * @param revisionIterator the iterator to be tested. + * @param name the expected name of the Person referenced by the Revision. + * @param revisionType the type of the revision denoting if it represents an insert, update or delete. + */ + private void checkNextRevision(Iterator> revisionIterator, String name, + RevisionType revisionType) { + + assertThat(revisionIterator.hasNext()).isTrue(); + Revision revision = revisionIterator.next(); + assertThat(revision.getEntity().name).isEqualTo(name); + assertThat(revision.getMetadata().getRevisionType()).isEqualTo(revisionType); + } + + /** + * Creates a Person with a couple of changes so it has a non-trivial revision history. + * @return the created Person. + */ + 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; + } +} +---- +<1> This references the application context configuration presented above +==== + +[[envers.resources]] +== Further Resources + +There is a https://github.com/spring-projects/spring-data-examples[Spring Data Envers example in the Spring Data Examples repository] that you can download and play around with to get a feel for how the library works. + +You should also check out the https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/history/RevisionRepository.html[Javadoc for `RevisionRepository`] and related classes. + +Questions are best asked at https://stackoverflow.com/questions/tagged/spring-data-envers[Stackoverflow using the `spring-data-envers` tag]. + +The https://github.com/spring-projects/spring-data-envers[source code and issue tracker for Spring Data Envers is hosted at GitHub]. + + diff --git a/src/main/asciidoc/index.adoc b/src/main/asciidoc/index.adoc index 5daaab0c..a47a8025 100644 --- a/src/main/asciidoc/index.adoc +++ b/src/main/asciidoc/index.adoc @@ -1,9 +1,10 @@ = Spring Data Envers - Reference Documentation -Oliver Gierke; +Oliver Gierke; Jens Schauder :revnumber: {version} :revdate: {localdate} +:javadoc-base: https://docs.spring.io/spring-data/envers/docs/{revnumber}/api/ :spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc - +:spring-data-commons-javadoc-base: https://docs.spring.io/spring-data/commons/docs/current/api/ (C) 2008-2021 The original authors. NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically. @@ -14,13 +15,13 @@ include::{spring-data-commons-docs}/dependencies.adoc[leveloffset=+1] include::{spring-data-commons-docs}/repositories.adoc[leveloffset=+1] [[reference]] -= Reference Documentation +== Reference Documentation -== TODO +include::envers.adoc[leveloffset=+1] [[appendix]] -= Appendix +== Appendix :numbered!: include::{spring-data-commons-docs}/repository-namespace-reference.adoc[leveloffset=+1]