How to implement AuditorAware with Spring Data JPA and Spring Security?

前端 未结 4 1569
星月不相逢
星月不相逢 2021-02-01 19:29

We use Hibernate/JPA, Spring, Spring Data and Spring Security in our application. I have a standard User entity which is mapped using JPA. Further, I have a U

4条回答
  •  被撕碎了的回忆
    2021-02-01 20:08

    It looks like you use a User entity for two different things:

    • authentication
    • audit

    I think it will be better to prepare a special AuditableUser for audit purpose (it will have identical username field as original User). Consider following case: you want to delete some User from database. If all your audit objects are linked to User then they will a) loose author b) may be deleted by cascade too (depends on how the link is implemented). Not sure that you want it. So by using special AuditableUser you will have:

    • no recursion
    • ability to delete some User from the system and preserve all audit info about it

提交回复
热议问题