2

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 (e.g. 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?

Community
  • 1
  • 1
Med
  • 628
  • 9
  • 29

1 Answers1

1

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

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Disabling the cache doesn't solve the issue: forum's new position is persisted in the database, but the list doesn't change. – Med Nov 07 '13 at 09:02
  • I think this is exactly my second question here : I feel a bit stupid, but I don't see what I should do to update my list. Since I didn't delete or add an element to my list, but only changed one already existing, how should I manually tell my list to reflect this change? – Med Nov 07 '13 at 09:06
  • My bad, your trick does work! Turns out the two environments I was using to test with Hibernate and EclipseLink didn't only differ at the persistence level, but also at the view level (different versions of JSF's mojarra). The one with EclipseLink had an old version of mojarra, containing a bug about view states in nested loops. I'm lucky I realized that, I would have taken me weeks to find out otherwise! Now everything works fine when I disable the cache. **Does it mean that if I want to use this L2 cache, I need to find another way to update my objects?** – Med Nov 07 '13 at 09:57
  • I'd create a new question to target specific cache questions. – membersound Nov 07 '13 at 13:12