跳至主要内容

JPA Data Auditing


Auditing

Sometime we need to keep track with the change of the Entity, eg.
  • When it is created
  • Who created it
  • Last modified date
  • Last modified by somebody

Implement via JPA

JPA specification provides a complete lifecycle of an Entity, which is easy to archive those purposes.
An example(dummy codes).
@Entity
public class Conference{
 
 private User createdBy;
 private Date createdDate;
 private user lastModifiedBy;
 private Date lastModifiedDate;
 
 
 @PrePersist
 public void prePersist(){
  setCreatedBy(currentUser);
  setCreatedDate(new Date());
 }
 
 @PreUpdate
 public void preUpdate(){
  setLastModifiedBy(currentUser);
  setLastModifiedDate(new Date());
 }

}
In this example, the method prePersist annotated with @PrePresistindicates it will be executed before em.persist(em is stand for jpa EntityManager) is executed.
And @PreUpdate will be executed before em.merge is executed.
JPA also provides a EntityListener feature. Using EntityListener, the lifecycle hook methods can be placed in a centered class.
Here we created two base entity classes, PersistableEntity identifies the entity can be persisted, it includes a id and version property. AndAuditableEntity extends PersistableEntity, adds some addtional properties.
@MappedSuperclass
public class PersistableEntity implements Serializable{
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id")
 private Long id;
 
 @Version
 private Long version;
}
@MappedSuperclass
@EntityListeners(value = { AuditEntityListener.class })
public class AuditableEntity extends PersistableEntity{

 @ManyToOne
 @JoinColumn(name = "created_by")
 private User createdBy;

 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "created_date")
 private Date createdDate;

 @ManyToOne
 @JoinColumn(name = "last_modified_by")
 private User lastModifiedBy;

 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "last_modified_date")
 private Date lastModifiedDate;
}
AuditableEntity is the base Entity class which defines the all auditing properties. It will apply an EntityListener AuditEntityListener class at runtime.
public class AuditEntityListener {
 private static final Logger log = LoggerFactory
   .getLogger(AuditEntityListener.class);

 @PrePersist
 public void prePersist(AuditableEntity e) {

  e.setCreatedBy(SecurityUtil.getCurrentUser());
  e.setCreatedDate(new DateTime());

 }

 @PreUpdate
 public void preUpdate(AuditableEntity e) {
  e.setLastModifiedBy(SecurityUtil.getCurrentUser());
  e.setLastModifiedDate(new DateTime());
 }
}
The @PrePersist and @PreUpdate methods can accept an object which will be passed into the related method of EntityManager.
Any classes extends AuditableEntity will be listened byAuditEntityListener.
@Entity
public class Signup extends AuditableEntity {
}
Obviously, the second solution is more flexible and maintainable.

Auditing from Spring Data JPA

Spring Data Commons defines some classes for auditing, currently only the Spring Data JPA module provides complete implementation.

Utilize the Spring Data facility

Spring Data provides two basic interfaces.
  • Persistable indicates the class is a persistable class, anyPersistable class could be indentified by a id property.
  • Auditable is an interface defines all required properties for auditing an Entity, it is inherited from Presistable interface.
Spring Data JPA provides two abstract classes to implement these interfaces.
  • AbstractPersistable implements Persisteable, annotates idproperty with @Id to indicate it is a JPA primary key.
  • AbstractAuditable implements Auditable, and the auditor type is a acceptable type argument passed into AbstractAuditable.
An example to demonstrate these.
@Entity
public class Conference extends AbstractAuditable<User, Long>{

}
Spring Data JPA provides a generic EntityListener to track theAuditable interfaces.
You must configure it in the orm.xml(/src/main/resouces).
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <entity-listeners>
                <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
            </entity-listeners>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>
You have to create a empty persistence.xml in the same folder, or theAuditingEntityListener does not work as expected, even you have declared a LocalContainerEntityManagerFactoryBean which does not force you to provide a persistence.xml configuration. Maybe this is an issue of Spring.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 
 xmlns="http://java.sun.com/xml/ns/persistence" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"/>
</persistence>
Next, you must create an AuditorWare implementation bean which implements how to fetch auditor, generally it is the current user in the real world application. Spring will inject the auditor into AuditingEntityListener at runtime.
@Named(value="auditorBean")
public class AuditorBean implements AuditorAware<User> {
 private static final Logger LOGGER=LoggerFactory.getLogger(AuditorBean.class);

 private User currentAuditor;
 
 @Override
 public User getCurrentAuditor() {
//  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//     if (authentication == null || !authentication.isAuthenticated()) {
//       return null;
//     }
//
//     return ((MyUserDetails) authentication.getPrincipal()).getUser();
  LOGGER.debug("call AuditorAware.getCurrentAuditor(");
  return currentAuditor;
 }

 public void setCurrentAuditor(User currentAuditor) {
  this.currentAuditor = currentAuditor;
 }
 
}
In the real world application, the current authenticated user can be fetched from SecurtiyContextHolder.
Declare a <jpa:auditing/> in Spring configuration to enable auditing feature in the project.
<jpa:auditing auditor-aware-ref="auditorBean"/>
Now write some codes to test the auditing.
@Test
@Transactional
public void retrieveConference() {
 Conference conference = newConference();
 conference.setSlug("test-jud");
 conference.setName("Test JUD");
 conference.getAddress().setCountry("US");
 conference = conferenceRepository.save(conference);
 em.flush();

 assertTrue(null != conference.getId());
 conference = conferenceRepository.findBySlug("test-jud");

 log.debug("conference @" + conference);
 assertTrue(null != conference);

 assertTrue(conference.getCreatedBy() != null);
 assertTrue("hantsy".equals(conference.getCreatedBy().getUsername()));
 assertTrue(conference.getCreatedDate() != null);
 assertTrue(conference.getLastModifiedBy() != null);

 assertTrue("hantsy"
   .equals(conference.getLastModifiedBy().getUsername()));
 assertTrue(conference.getLastModifiedDate() != null);

 assertTrue(conference.getCreatedDate().equals(
   conference.getLastModifiedDate()));

 conference
   .setDescription("change desc, the modified date should be updated.");
 conferenceRepository.save(conference);
 em.flush();

 conference = conferenceRepository.findBySlug("test-jud");
 log.debug("created date @" + conference.getCreatedDate());
 log.debug("modified date @" + conference.getLastModifiedDate());
 assertTrue(!conference.getCreatedDate().equals(
   conference.getLastModifiedDate()));
}

Add auditing to existing classes

If you have some existing entities, and do not modify them to extendAbstractAuditable, you can add some annotations to existing classes to implement the auditing feature.
@Entity
public class Signup {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id")
 private Long id;

 @DateTimeFormat(style = "M-")
 @CreatedDate
 private Date createdDate;
 
 @LastModifiedDate
 @DateTimeFormat(style = "M-")
 private Date modifiedDate;
 
 @CreatedBy()
 @ManyToOne
 private User createdBy;
 
 @LastModifiedBy
 @ManyToOne
 private User modifiedBy;

}
The date fields can accept java.util.Date, joda DateTime orLong/long type.
Here some test codes.
@Test
@Transactional
public void testSignup() {
 Conference conference = newConference();
 conference.setSlug("test-jud");
 conference.setName("Test JUD");
 conference.getAddress().setCountry("US");
 Signup signup = newSignup();
 conference.addSignup(signup);
 conference = conferenceRepository.save(conference);
 em.flush();

 assertTrue(null != conference.getId());
 assertTrue(null != signup.getId());

 Signup signup2=signupRepository.findById(signup.getId());
 
 assertTrue(signup2.getCreatedBy() != null);
 assertTrue("hantsy".equals(signup2.getCreatedBy().getUsername()));
 assertTrue(signup2.getCreatedDate() != null);
 assertTrue(signup2.getModifiedBy() != null);

 assertTrue("hantsy"
   .equals(signup2.getModifiedBy().getUsername()));
 assertTrue(signup2.getModifiedDate() != null);

 assertTrue(signup2.getCreatedDate().equals(
   signup2.getModifiedDate()));

 signup2.setComment("add comment to signup, and the signup is changed, the modified date should be updated.");
 signupRepository.save(signup2);
 em.flush();

 signup2=signupRepository.findById(signup.getId());
 log.debug("created date @" + signup2.getCreatedDate());
 log.debug("modified date @" + signup2.getModifiedDate());
 assertTrue(!signup2.getCreatedDate().equals(
   signup2.getModifiedDate()));
}

Summary

The Auditing feature from Spring Data JPA is very simple and stupid, and it is useful in the real world application.

评论

此博客中的热门博文

Build a Reactive application with Angular 5 and Spring Boot 2.0

I have created a post to describe Reactive programming supports in Spring 5 and its subprojects, all codes of this article are updated the latest Spring 5 RELEASE, check spring-reactive-sample under my Github account.
In this post, I will create a simple blog system, including:
A user can sign in and sign out.An authenticated user can create a post.An authenticated user can update a post.Only the user who has ADMIN role can delete a post.All users(including anonymous users) can view post list and post details.An authenticated user can add his comments to a certain post. The backend will be built with the latest Spring 5 reactive stack, including:
Spring Boot 2.0, at the moment the latest version is 2.0.0.M7Spring Data MongoDB supports reactive operations for MongoDBSpring Session adds reactive support for WebSessionSpring Security 5 aligns with Spring 5 reactive stack The frontend is an Angular based SPA and it will be generated by Angular CLI.
The source code is hosted on Github, …

Activating CDI in JSF 2.3

Activating CDI in JSF 2.3 When I upgraed my Java EE 7 sample to the newest Java EE 8, the first thing confused me is the CDI beans are not recoganized in Facelects template in a JSF 2.3 based web applicaiton, which is working in the development version, but in the final release version, they are always resolved as null. I filed an issue on Mojarra and discussed it with the developers from communities and the JSF experts.
According to the content of README, In a JSF 2.3 application, to activate CDI support, declaring a 2.3 versioned faces-config.xml and adding javax.faces.ENABLE_CDI_RESOLVER_CHAIN in web.xml is not enough, you have to declare @FacesConfig annotated class to enable CDI.
Here is the steps I created a workable JSF 2.3 applicatoin in Java EE 8.
Create a Java web application, this can be done easily by NetBeans IDE, or generated by Maven archetype, for exmaple.
$ mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-w…

JSF 2.3:Websocket support

Websocket support One of the most attractive features is JSF 2.3 added native websocket support, it means you can write real-time applications with JSF and no need extra effort.
To enable websocket support, you have to add javax.faces.ENABLE_WEBSOCKET_ENDPOINT in web.xml.
<context-param> <param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name> <param-value>true</param-value> </context-param> Hello Websocket Let's start with a simple example.
@ViewScoped@Named("helloBean") publicclassHelloBeanimplementsSerializable { privatestaticfinalLoggerLOG=Logger.getLogger(HelloBean.class.getName()); @Inject@PushPushContext helloChannel; String message; publicvoidsendMessage() { LOG.log(Level.INFO, "send push message"); this.sendPushMessage("hello"); } privatevoidsendPushMessage(Objectmessage) { helloChannel.send(""+ message +" at &…