-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Evicting orphans from session.refresh()
PostPosted: Mon Jan 18, 2016 9:13 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Ok so we have a bit of an odd situation with Hibernate and I hope I can depict the problem correctly.

When we refresh an entity using session.refresh(), orphans can be left in the session that will then cause a staleobjectstateexception on the next flush.

Here's my example scenario:
  • I have an entity called Human that has a cascading (Cascade All) association called Arm. One Human normally has 2 Arms.
  • In our database we have Human1 that has Arm1 and Arm2.
  • We have 2 users using the application (User 1 and User2).
  • User1 and User2 both have open hibernate sessions (Session1 and Session 2). These open sessions contains the Human and its arms
  • User1 from Session1 removes Arm1 from Human1 (sick bastard I know) and flushes.
  • User2 from Session2 calls refresh() to reload the Human1 changes (makes other modifications) and flushes

Here's the problem: User2 will get a StaleOjectNotFoundException. Why? Because Arm1 is still in the session2. Yes we refreshed the Human1 but since Arm1 is "disconnected" it stays in the session...and when we try to refresh, hibernate isn't happy with this entity that was previously deleted.

Obviously my application is way more complex. We manage a very large tree of entities. We also use the OpenSessionInView paradigm as you probably guessed.

Here are a few ideas we had to solve the problem but we're not sure they're feasable or the best way to move forward:
  • Is it possible to modify the Hibernate on flush listener to ignore those 'orphans'? Is there any way to know that they're 'orphans'...
  • Can we have logic prepending the refresh that will go evict the orphans one way or another...? This is rather complicated when you have a large tree of dependant entities that are sometimes bi-directional.
  • Can we make our own 'cascading' refresh that will somehow manage this?

Have any of you ever had to face this situation? Or do you have any idea how we can go to solve this problem?

Thanks a lot,

PS: We're using Hibernate 3.6


Top
 Profile  
 
 Post subject: Re: Evicting orphans from session.refresh()
PostPosted: Tue Jan 19, 2016 1:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I think the most natural solution is to use merge instead of reattach.
With merge you copy the detached entity onto a managed entity, so if the detached doesn't have some children, those will be marked as orphans and deleted.


Top
 Profile  
 
 Post subject: Re: Evicting orphans from session.refresh()
PostPosted: Tue Jan 19, 2016 12:42 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hi.

Thanks for the reply but we use OpenSessionInView while keeping our entities attached.

After more analysis I realize the problem comes from our own implementations. Apparently we were "nulling" the associations manually. This is why it was dissociated from the hibernate session. By evicting the object as we're nulling the association, the problem is solved.

Thanks for your time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.