-->
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.  [ 6 posts ] 
Author Message
 Post subject: Good usage to update an object
PostPosted: Sun Feb 06, 2005 5:57 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Hibernate version: Hibernate 3.0 beta3

What the good way to do that (i just wrote the logic of my business) :

Code:
open hibernate session

while true
  component = read data from a stream
  storedComponent = query to check if the read component already exists
  if component is new
    session.save(component)
  else
    i have to update the component data and store it into database
    (my question comes here)
  endif
  flush hibernate session
end while

? close hibernate session ?


My component has a collection of A (and each A has a collection of B). How update my stored component to correctly consider all A and B children ? :
storedComponent.getAs().removeAll();
storedComponent.getAs().addAll(component.getAs());
With this code, i'll have many B orphans, and the autoincrement of B will grow up quickly.

Any idea ? (i hope to be clear ;-)

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 06, 2005 6:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
session.saveOrUpdate()


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 06, 2005 6:31 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Ok, so i do that:

// id contains the primary key used by hibernate
readComponent.setId(storedComponent.getId());
session.saveOrUpdate(readComponent);

and all children (and children of children) will be removed (i configured cascade="delete-all-orphan" for one-to-many), and the new one will be added. I've right ?

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 06, 2005 6:54 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Apparently, i have wrong :-/

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

_________________
Vincent


Top
 Profile  
 
 Post subject: Were you able to solve the exception?
PostPosted: Thu Feb 17, 2005 8:53 am 
Newbie

Joined: Tue Jan 25, 2005 8:37 am
Posts: 2
I have the same problem now myself...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 3:48 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
I had the object get from hibernate and an other 'image' of this object read from an external stream. I had merge data from the 'streamed' object onto the persitent object, then i updated the persistent object. Now works fine.

_________________
Vincent


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.