// Other code omitted
@PersistenceContext
private EntityManager entityManager;
@Transactional(readOnly=true)
public ContactAudit findAuditByRevision(Long id, int revision) {
AuditReader auditReader = AuditReaderFactory.get(entityManager);
return auditReader.find(ContactAudit.class, id, revision);
}
}
As shown in Listing 10-51, the EntityManager was injected into the class, which was passed to the
AuditReaderFactory to retrieve an instance of AuditReader. Then we can then call the
AuditReader.find() method to retrieve the instance of the ContactAudit entity at a particular revision.
Testing Entity Versioning
Let's take a look at how entity versioning works. Listing 10-52 shows the testing code snippet. In Listing
10-52, the code for bootstrapping ApplicationContext and the listContacts() function is the same as
the one in the SpringJpaSample class.
Listing 10-52. Testing Entity Versioning
package com.apress.prospring3.ch10;
// Import statements omitted
public class SpringJpaAuditSample {
public static void main(String[] args) {
// Other code omitted
// Add new contact
System.out.println("Add new contact");
ContactAudit contact = new ContactAudit();
contact.setFirstName("Michael");
contact.setLastName("Jackson");
contact.setBirthDate(new Date());
contactService.save(contact);
contacts = contactService.findAll();
listContacts(contacts);
// Update contact
System.out.println("Update contact");
contact.setFirstName("Tom");
contactService.save(contact);
contacts = contactService.findAll();
listContacts(contacts);
// Find audit record by revision
ContactAudit oldContact = contactService.findAuditByRevision(1l, 1);
System.out.println("");
System.out.println("Old Contact with id 1 and rev 1:" + oldContact);
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home