Usage
现在你可以使用 RevisionRepository
的方法来查询实体的修订,如下所示:
You can now use the methods from RevisionRepository
to query the revisions of the entity, as the following test case shows:
@ExtendWith(SpringExtension.class)
@Import(EnversDemoConfiguration.class) (1)
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<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();
}
/**
* 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.
* <p>
* 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<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);
}
/**
* 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 earlier (in the Configuration section). |
Further Resources
可以下载 Spring Data Envers example in the Spring Data Examples repository 并进行试用,以了解库的工作原理。
You can download the Spring Data Envers example in the Spring Data Examples repository and play around with to get a feel for how the library works.
您还应该查看 RevisionRepository
的 Javadoc 以及相关类目 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/history/RevisionRepository.html。
You should also check out the Javadoc for RevisionRepository
and related classes.
您可以在 link:https://stackoverflow.com/questions/tagged/spring-data-envers[Stackoverflow by using the spring-data-envers
标签中提问。
You can ask questions at Stackoverflow by using the spring-data-envers
tag.
source code and issue tracker for Spring Data Envers is hosted at GitHub(作为 Spring Data JPA 的模块)。
The source code and issue tracker for Spring Data Envers is hosted at GitHub (as a module of Spring Data JPA).