Auditing entities in Spring Data MongoDB
3606 ワード
もっと読む
Spring Data MongoDB 1.2.0 silently introduced new feature:support for baic audiiting.Because you will not find too much about it in offical reference in this post I will show ahat benefits does it bring,how to configre Spring for auding and how to annotate your document to make the make aditing.Auditing. let you declarabiely tell Spring to store: date when document has been created: @CreatedDate date when document has been udated last time: @LastModified Date user who has created document: @CreatedBy user who has done most recent udate: @LastModifieBy current document version: @Version Configration
First of all Maven dependencies to latest Spring Data MongoDB and Spring Data Comons.Additionally in order to use date-related audit annotations we need to add jda-time トクラスパス.
Configration above provides us way for auditing that includes versioning and timestamps.Example document will look like:
As YOU have probably noticed I did not use here user related annotations @CreatedBy and @LastModifieBy.In order to use them we need to tell Spring who is a current user.
First add user related fields to your audited class:
udateの場合はトリガされないようですが、saveの場合はトリガされます.この場合はmongo Template.save(S entity)を使用して、データが存在する場合は修正操作が実行されます.
Spring Data MongoDB 1.2.0 silently introduced new feature:support for baic audiiting.Because you will not find too much about it in offical reference in this post I will show ahat benefits does it bring,how to configre Spring for auding and how to annotate your document to make the make aditing.Auditing. let you declarabiely tell Spring to store:
First of all Maven dependencies to latest Spring Data MongoDB and Spring Data Comons.Additionally in order to use date-related audit annotations we need to add jda-time トクラスパス.
org.springframework.data
spring-data-mongodb
1.2.1.RELEASE
org.springframework.data
spring-data-commons
1.5.1.RELEASE
joda-time
joda-time
2.2
In order to enable auditing we need to add トSpring configration.Currenntly there isのway to configre it through Java Config.
UsageConfigration above provides us way for auditing that includes versioning and timestamps.Example document will look like:
@Document
public class Item {
@Id
private String id;
@Version
private Long version;
@CreatedDate
private DateTime createdAt;
@LastModifiedDate
private DateTime lastModified;
...
}
Now you can save document using Mongo Template or your repository and all annotated fields are atomagically set.As YOU have probably noticed I did not use here user related annotations @CreatedBy and @LastModifieBy.In order to use them we need to tell Spring who is a current user.
First add user related fields to your audited class:
@CreatedBy
private String createdBy;
@LastModifiedBy
private String lastModifiedBy;
The n create your implemention of Auditor Aware that will obtain current user(probably from session or Spring Security context–depends on your appication):public class MyAppAuditor implements AuditorAware {
@Override
public String getCurrentAuditor() {
// get your user name here
return "John Doe";
}
}
Last thing is to tell Spring Data MongoDB about this auditor aware class by little modification in Mongo configration:
udateの場合はトリガされないようですが、saveの場合はトリガされます.この場合はmongo Template.save(S entity)を使用して、データが存在する場合は修正操作が実行されます.