JPA Hibernate intermediary table - resubmitted

爷,独闯天下 提交于 2019-12-13 04:40:51

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!