JPA Hibernate intermediary table - resubmitted

2019-09-14 03:59发布

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 Users to Roles. 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条回答
Summer. ? 凉城
2楼-- · 2019-09-14 04:35

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.

查看更多
登录 后发表回答