问题
Rephrase of original question:
I have your typical M:M relationship, in my case think of the User
/Role
example:
USER, ROLE, USER_ROLE
I need a second USER_ROLE table matching up User
s to Role
s. I won't get into specifics as to why that is (unless you need me to), but I am looking for a way to accomplish this with JPA AND without changing up the User
and Role
Entities.
*I need a second USER_ROLE table to audit actions the User
performs and which Role
he was at the time the action was performed.*
回答1:
You may want to use a internal callback method that will create an entry in the audit log whenever the lifecycle of the entity changes (according to your needs).
The callback annotations are self-explanatory more or less:
@PrePersist void onPrePersist() {}
@PostPersist void onPostPersist() {}
@PostLoad void onPostLoad() {}
@PreUpdate void onPreUpdate() {}
@PostUpdate void onPostUpdate() {}
@PreRemove void onPreRemove() {}
@PostRemove void onPostRemove() {}
I quote from the documentation of the annotations:
Is used to specify callback methods for the corresponding lifecycle event. This annotation may be applied to methods of an entity class, a mapped superclass, or a callback listener class.
来源:https://stackoverflow.com/questions/22352205/jpa-intermediary-table