我的AuditListener
public class EmployeeAuditListeners {
@PrePersist
public void prePersist(Employee employee){
perform(employee,Action.INSERTED);
}
@PreUpdate
public void preUpdate(Employee employee){
perform(employee,Action.UPDATED);
}
@PreRemove
public void preRemove(Employee employee){
perform(employee,Action.DELETED);
}
@Transactional
public void perform(Employee emp, Action action){
EntityManager em = BeanUtil.getBean(EntityManager.class);
CommonLogs commonLogs = new CommonLogs();
commonLogs.setQuery("new query");
em.persist(commonLogs);
}
}
和我的 Auditable.class
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@CreatedBy
protected U createdBy;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
protected Date createdDate;
@LastModifiedBy
protected U lastModifiedBy;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
protected Date lastModifiedDate;
}
我的 CommonLogs.class
@Entity
@EntityListeners(AuditingEntityListener.class)
public class CommonLogs extends Auditable<String> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String query;
public CommonLogs() {
}
public CommonLogs(String query) {
this.query = query;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
我的Employee.java类
@Entity
@EntityListeners(EmployeeAuditListeners.class)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
我有一个简单的Rest Controller
@RestController
@RequestMapping("/api")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee employee){
return employeeRepository.save(employee);
}
}
我想在每次对Employee实体执行一些操作时都将其记录在我的表(common_logs)上。
上面给出的示例在某种程度上可以正常工作,因为它成功存储了雇员并调用了EmployeeAuditListeners。
但是现在在保存CommongLog实体时,我希望它的父类Auditable可自动插入createdBy,createdDate等。目前,只有查询和ID插入common_logs表中,其余列为空。
答案 0 :(得分:0)
您可以在here中查看审核文档。
要启用自动审核,必须在Application类中添加注释@EnableJpaAuditing
:
@SpringBootApplication
@EnableJpaAuditing
class Application {
static void main(String[] args) {
SpringApplication.run(Application.class, args)
}
}
如果您还需要字段@CreatedBy
和@LastModifiedBy
,则还需要实现AuditorAware<T>
接口。例如:
class SpringSecurityAuditorAware implements AuditorAware<User> {
public User getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
}
return ((MyUserDetails) authentication.getPrincipal()).getUser();
}
}