The reason why we use Jpa Auditing is that we automatically save created_by, created_at, updated_by, updated_at.
Recording them in DB is important.
Add annotation
@Entity
@EntityLsteners({AuditingEntityListener.class})
@CreatedBy => when insert quary executes, created_by automatically saves in db.
@CreatedDate => when insert quary executes, created_at automatically saves in db.
@LastModifiedBy => when insert and update quary execute, updated_by automatically saves in db.
@LastModifiedDate => when insert and update quary execute, updated_at automatically saves in db.
@EntityListeners({DefaultEntityListener.class, AuditingEntityListener.class})
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class User{
/* fields */
@CreatedBy
private String createdBy;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedBy
private String updatedBy;
@LastModifiedDate
private LocalDateTime updatedAt;
}
Add @EnableJpaAuditing in Application.java
@EnableJpaAuditing
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Write what will be saved in created_by and updated_by in getCurrentAuditor() method.
@Component
public class AuditAware implements AuditorAware<String>{
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("SChoi005");
}
}
Entity listeners are useful for general purpose auditing or logging applications.
@PrePersist // Before Insert query
@PreUpdate // Before Update query
@PreRemove // Before Delete query
@PostPersist // After Insert query
@PostUpdate // After Update query
@PostRemove // After Delete query
@PostLoad // After select query