-->
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.  [ 7 posts ] 
Author Message
 Post subject: Saving or updating an object
PostPosted: Mon Oct 03, 2005 5:37 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
Hibernate version:
3.0.5

I have trouble understanding how the following should work and although I've digg around I haven't been able to find an answer.

Assume the following table structure
Code:
person
-------------------------
id (long), name (unique string)
-------------------------


Assume we have a transient object we would like to save or update.

Code:
Person p = new Person("foo");
session.saveOrUpdate(p);


Now this object has an id of type Long which is null, therefore the object is saved instead of being updated.
Now, if we try to save the object again we'll get an exception (duplicate key violates unique constraint) on name.

Now the way I undertand it is that I everytime I need to save a Person object, I have to check if it already exists in the database to avoid getting an exception.

Also, in case that my Person object references other objects that I need to cascade the save or update function, I also have to check if they do exist in the database. Of course all this becomes really tedious and can lead to other problems as well.

Before I go on, is my assumption correct or am I missing something?

Thanks for your input.

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 10:41 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
When you saveOrUpdate an object, it examines it's id to see if it's unsaved or saved. If unsaved, it creates it, and returns the new id. If it is already saved, it updates the persistent form with the changes that have been introduced.

If you create a Person:

Code:
Person p = new Person();
p.setName("Example");
Serializable id = session.saveOrUpdate(p);


Then a persistent form is created for that person. If you do:

Code:
Person p = new Person();
p.setName("Example");
Serializable id = session.saveOrUpdate(p);
id = session.saveOrUpdate(p);


Then you are trying to create two people with the name "Example". However, if you do:

Code:
Person p = new Person();
p.setName("Example");
Serializable id = session.saveOrUpdate(p);
p = (Person)session.load(Person.class, id);
id = session.saveOrUpdate(p);


Then you are saving the person, loading up the person with the correct id, and trying to updated the persisted form with that person (which does nothing).

So, you do not have to check existence unless you want to avoid exceptions due to unique constraints. In the case that you do, then as you say, you will have to recurse down the children and associations. Since this is probably alot of work, it may be easier to let the db do this for you, and respond with an exception when yu try to save.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:50 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
Hey CollinL and thanks for your reply!

The problem is that I obtain the Person objects through a service. I go ahead and save them (since its the first time I do this there is no problem!).

Now, the second time I ask for the Person objects some of them could already be in my database therefore creating the problem I mentioned!

The way I think about it is that everytime I have to merge the persistent Person with the one obtaining by the service.
Is that the way to do things?

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 12:37 pm 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
Maybe http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-replicating could be useful?

Otherwise, I'm guessing that you'll end up with having to traverse, as you said, to merge the separate object graphs together.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:13 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
Thanks for looking into it CollinL

As far as I can see it seems that replicate overwrites the row instead of saving it if it does not exist therefore it's not what I want.

the merge javadoc states that
Quote:
If the given instance is unsaved, save a copy of and return it as a newly persistent instance.


I guess Ill try merge.

I'm surprised no one else seems to be interested in this.

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 7:14 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
No luck...I tried it and I get an Unsupported operation exception on Collections.

I also tried the following,

1. load the persisted object
2. use its id to set it as the id of the transient object
3. evict the peristed object

But I ran into many "org.hibernate.NonUniqueObjectException" exceptions since its rather difficult to trace all the references in the relationships plus it doesn't sound really the correct way of doing things.

So I'm stuck again! I've tried different things and patterns multiplied by the amount of time spent on debugging and still I can't think of any feasible solution.

I'd really appreciated if anyone who has run into this problem before can elaborate.

Thanks

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 5:19 am 
Newbie

Joined: Wed Sep 05, 2007 2:26 am
Posts: 5
Hello Cue,

As i see, this, your post is couple years back: now i'm
facing with almost exactly the same problem. Did you
get any idea or approach on how this can be solved?

I have created a topic in here for this:
http://forum.hibernate.org/viewtopic.php?t=980579

Thanks and Regards,
Sanjeev


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