未调用JPA EventListener方法更改为多对多集合? [英] JPA EventListener method not called on change to many-to-many collection?

查看:119
本文介绍了未调用JPA EventListener方法更改为多对多集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用 EntityListener s作为在我的Spring webapp中的实现。审计(Spring-data)和其他通知目的。一般来说,它工作正常。但是,在对多对多集合/关系进行更改时,如果没有对实体自身的任何字段进行任何其他更改, @PostUpdate 事件 EventListener s没有被触发。



举个具体的例子:我有一个用户和一个角色实体,它们之间具有多对多的关系,使用基础表。如果我转到我的用户管理GUI并向用户添加(或删除)角色而不更改其他任何内容,则不会触发postUpdate事件,审核信息不会被修改。



它有一些逻辑(低级别),因为用户或角色表都没有更新,只有关系表是。然而,关系本身并没有被建模为一个实体类,因此不可能对它进行审计,至少不是在JPA级别,所以触发实体本身的变更事件是有意义的。



这是正常的JPA行为,还是特定于Hibernate?有没有解决方法?你会怎么做才能触发这个事件?想法?



注意:我发现很少提到这个限制,甚至更少的解决方案:
这个SO问题(没有有用的答案)和在Hibernate论坛上的这篇文章也没有任何答案。

JPA规范对集合的更改非常不清楚,这些更改触发拥有它们的实体的前/后更新回调。由于不清楚,我的意思是完全沉默。事实上,IMO规范中的措辞意味着回调不应该发生,因为前/后更新应该发生在为该实体发送SQL UPDATE语句之前/之后 。现在,如果拥有实体是版本化的,那么Hibernate会对它拥有的任何集合(真正的任何属性)的更改进行处理,以强制所有实体上的版本增量,这将触发更新该实体将会触发更新后的回调。

I am using JPA with Hibernate as the implementation in my Spring webapp.

I use EntityListeners for auditing (Spring-data) and other notification purposes. In general it works fine. However, when changes are made to a many-to-many collection/relationship, without any other change to any field of the entities themselves, the @PostUpdate event of the EventListeners are not fired.

To give a concrete example : I have a User and a Role entities, with a many-to-many relationship between them, using an underlying table. If I go to my user management GUI, and add (or remove) roles to a user, without changing anything else, the "postUpdate" event is not triggered, the auditing information is not modified.

There is some logic to it (at a low level) since neither the user or the role tables are updated, only the relationship table is. However the relationship itself is not modeled as an entity class and so it is impossible to apply auditing to it, at least not at the JPA level, so it would make sense to trigger the change event for the entity itself.

Is this a normal JPA behaviour, or is this specific to Hibernate ? Are there any workaround ? What would you do to trigger this event ? Thoughts ?

Note : I found very few mentions of this limitation, and even less solutions : This SO question (without useful answer) and this post on Hibernate's forum also without any answer.

解决方案

The JPA spec is very unclear about changes to collections triggering pre/post update callbacks on the entity that owns them. And by unclear, I mean completely silent. In fact, IMO the wording in the spec implies that the callback should not happen since pre/post update is supposed to happen before/after SQL UPDATE statements are sent for that entity.

Now, if the owning entity is versioned Hibernate treats changes to any collections (any attributes really) it owns as forcing a version increment on the owning entity which will trigger the update of the entity which will in turn trigger the post-update callback.

这篇关于未调用JPA EventListener方法更改为多对多集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆