简体   繁体   中英

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.

My issue is that the change is well persisted in the database, but is not reflected on the OneToMany mapped 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 ) . 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. 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) ?

[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"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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