-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problems with SaveOrUpdate
PostPosted: Thu Oct 19, 2006 7:52 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hello everybody,

I have a question about the saving and/or updating mechanism of hibernate.

In my application I have a lot of tables which are all mapped bidirectional.
In most cases I have a <one-to-many> assosiation on the one of the sides and a <set> on the other side.
The <one-to-many> side usually holds the foreign key.

It can happen that I save a pojo containing a <set> to the database where the set is null. This does not conflict with any of the databse constrains.
Later on (long after the origianl session has been committed) I construct the pojos for the set and add them to the set.
When I commit this pojo now using
Code:
session.saveOrUpdate(pojo)

I expect the the part of the pojo which is already persistant is updated (cause these table entries already exist) and the newly created pojos are inserted.

Interestingly this doesnt happen in my application. Hibernate wants to do an update on an not existing table entry which fails of course.

To make it clearer here a quick example:
I have Persons and phone numbers.
A person doesnt have to have a phone number, but it can have many.
An already "registered" person buys a phone and wants to register the phone numbner on its name.
In this case I have the Person pojo and say something like
Code:
PhoneNumber number = new PhoneNumber(new Long(234896423986489));  //lets say only the actual phone number is PK

if(person.getPhoneNumbers == null){ //create a new list of numbers
   person.setPhoneNumbers(new HashSet());
   person.getPhoneNumbers().add(number);
}
else{  //append to existing list
   person.getPhoneNumbers().add(number);
}
number.setPerson(person);  //set the person for the phone number


Now, lets say that the Persons ID was "33".
In this case my hibernate creates the sql:
    Update PHONENUMBER set PERSON_ID=33 where NUMBER=234896423986489

This is not right, cause there is no entry with NUMBER=234896423986489

Do you know what I'm trying to say?
Do you understand my problem?

Sorry I havent included any mappings, cause I did this quick and dirty from scratch.
I'll include some mapping files if you weant me to do so, but I think my problem is comprehensible without them.

Thank you very much!
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 9:37 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:44 pm
Posts: 33
Hi!

If I understand you correctly the problem is that the new PhoneNumber you created with id 234896423986489 isn't saved.

It can be that you need to a "save-update" cascade attribute to your hibernate mappingsfile for the relationship from person to number, to make hibernate save the numbers of a persn when you save the person.

See http://www.hibernate.org/116.html#A7


-Kaj :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 9:53 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hello Kai,

yes you understand correctly, the phone number ist not saved (Hibernate tries to update a non exiting entry instead).
It throws the following exception:
    org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1


I dont think its a problem with the cascade attribute, cause at the moment I have cascade="all" configured which should serve. Or am I mistaken here?

Thanks for your help.
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 10:31 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
It's not necessarily a cascade problem, but rather looks like a configuration problem. What is your unsaved-value attribute on the id node of the PHONENUMBER (if any)? Also, what is the generator class from the same mapping? Maybe just post the id node of the PHONENUMBER mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 10:52 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hello Ananasi,

thank you for pushing my face right into the center of the problem!
I had a mistake in the generation class. It was set to "native" when I wanted to have "assigned".

Thanks again!
Stefan


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