JPA Hibernate中介表 - 重新提交 [英] JPA Hibernate intermediary table - resubmitted
问题描述
我有你的典型M:M关系,在我看来,
USER,ROLE,USER_ROLE
我需要第二个USER_ROLE表,匹配 User
s到 Role
s。我不会详细说明这是为什么(除非你需要我),但我正在寻找一种方法来完成这与JPA和不改变用户
和角色
实体。
*我需要第二个USER_ROLE表来审核
您可能需要使用内部回调方法,当实体的生命周期发生变化时(根据您的需要),它将在审计日志中创建一个条目。
回调注释不言自明
@PrePersist void onPrePersist(){}
@PostPersist void onPostPersist(){}
@PostLoad void onPostLoad(){}
@PreUpdate void onPreUpdate(){}
@PostUpdate void onPostUpdate(){}
@PreRemove void onPreRemove (){}
@PostRemove void onPostRemove(){}
注释的一个href =http://docs.oracle.com/javaee/6/api/javax/persistence/PostUpdate.html =nofollow>文档:
用于指定相应生命周期
事件的回调方法。这个注解可以应用于实体类的方法,
映射超类或回调监听器类。
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.*
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.
这篇关于JPA Hibernate中介表 - 重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!