简体   繁体   English

JPA-如何在双向OneToMany / ManyToOne关系中更新集合

[英]JPA - How to update a collection in a bi-directionnal OneToMany/ManyToOne relation

I have an owner (Category) and a customer (Forum) entities designed this way: 我有一个这样设计的所有者(类别)和客户(论坛)实体:

Category 类别

...

@OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "category" )
@OrderBy( "position" )
private List<Forum> forums;

...

Forum 论坛

...

@ManyToOne
@JoinColumn( name = "category" )
private Category category;

private Integer        position;

...

Somewhere in my webapp, I display the forums list, and I execute some business action on one of them, for example changing its position. 在我的Web应用程序中的某个位置,显示论坛列表,并对其中之一执行一些业务操作,例如更改其位置。

My issue is that the change is well persisted in the database, but is not reflected on the OneToMany mapped collection . 我的问题是,更改已很好地保留在数据库中,但未反映在OneToMany映射的collection上 Thus when I refresh the page that displays the list, it doesn't show the new position but the previous one. 因此,当我刷新显示列表的页面时,它不会显示新位置,而是显示前一个位置。

The code I use to update an element's position is equivalent to a simple em.merge( forum ) . 我用来更新元素位置的代码等效于一个简单的em.merge( forum ) I read in many topics that I should "manually manage" the bidirectionnal relationship, otherwise I would face this kind of issues. 我读过许多主题,我应该“手动管理”双向关系,否则我将面临此类问题。 Here are my two questions : 这是我的两个问题:

  • I tried on both EclipseLink and Hibernate, and while it works well with Hibernate (list is updated), it doens't in EclipseLink. 我在EclipseLink和Hibernate上都进行了尝试,尽管它与Hibernate兼容(列表已更新),但在EclipseLink中却没有。 Is it related to some caching mode difference? 它与某些缓存模式的差异有关吗?
  • How should I manually notice my list that one of its element's property has changed (eg the position property of a forum in the forums list) ? 我应该如何手动注意到列表中其元素的一个属性已更改(例如,论坛在列表中的position属性)?

[EDIT] I read this topic and I feel my issue is related to my forum being updated by a different session from the one being used to fetch the list. [编辑]我读了这个话题 ,我觉得我的问题与论坛被另一个用于获取列表的会话所更新有关。 Am I understanding it right? 我理解正确吗? If yes, how to correctly manage that case? 是,如何正确处理?

Assuming you set both sides of the objects (you have to!), try disabling the eclipselink cache: <property name="eclipselink.cache.shared.default" value="false"/> 假设您同时设置了对象的两侧(必须这样做!),请尝试禁用eclipselink缓存: <property name="eclipselink.cache.shared.default" value="false"/>

@source http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F @source http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM