-->
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: one-to-many and "row not found"
PostPosted: Fri Jun 11, 2004 6:53 pm 
Newbie

Joined: Wed Jun 02, 2004 7:06 pm
Posts: 8
Hello, I would not ask, this seems to be a common issue, but I have been struggling with this for a week and feel that I am following the examples reasonably well. I am using Pointbase 4.8 and Hibernate 2.1.

I have a parent Person.hbm.xml that specifies

<set name="contacts" inverse="true" lazy="true" cascade="all">
<key column="PERSON_ID"/>
<one-to-many class="Contact"/>
</set>

The parent's child Contact.hbm.xml specifies

<many-to-one name="person" class="Person" column="PERSON_ID" not-null="true"/>

The test code is

session = sessionFactory.openSession();
transaction = session.beginTransaction();
person1 = new Person("100", ... new HashSet()); PK is "100"
session.save(person1);
contact1 = new Contact("I", ... person1); // PK is "I"
contact2 = new Contact("B", person1); // PK is "B"
person1.getContacts().add(contact1);
person1.getContacts().add(contact2);
session.flush();
transaction.commit(); // Commit the inserts.
closeSession(session);

I have tried many variations of this code (e.g. session.save(person1) instead of session.flush()) and basically at session.flush() I get

16:49:18,282 ERROR SessionImpl:2368 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)...

Is there an answer to this? Thank you, David


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 12, 2004 8:21 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Could you please post your full mappings for your Person and Contact classes - I think this is a problem with not setting the unsaved-value on the ID property on one of them.

As a seconday point, is there any reason you do the save before adding the contacts to the person? You also don't need to flush the session, as committing a transaction does that automatically.


Top
 Profile  
 
 Post subject: one-to-many and "row not found" - Thanks sdknott
PostPosted: Mon Jun 14, 2004 10:19 am 
Newbie

Joined: Wed Jun 02, 2004 7:06 pm
Posts: 8
Dear sdknott,

Your simple and clear explanation solved my problem. Contrary to the Hibernate/general DB design recommendations (and as I hinted in my post) I am using keys with business value. Now, in each of my two hbm.xml files I set unsaved-value="any" (the attribute defaulted to null, but I was instantiating the children with id values.) I realize that the "unsaved-value" attribute determines Hibernate's handling of objects between sessions, but I'll have to keep testing to see what the ramifications are.

Per your suggestion I eliminated session.flush() and moved the save of the parent object to just before transaction.commit(). The children now save automatically.

Thank you once again for taking the time to answer my post. You have started my week on a good note!


Top
 Profile  
 
 Post subject: there may be problems while updating with unsaved-value=&quo
PostPosted: Mon Jun 14, 2004 3:19 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
I have the same problem. Though using unsaved-value="any" you will be able to insert it successfully. But it fails when you do updates.

Please let me know if update works fine.

Thanks,
Jeelani


Top
 Profile  
 
 Post subject: You are right, sjeelani, about update
PostPosted: Mon Jun 14, 2004 4:07 pm 
Newbie

Joined: Wed Jun 02, 2004 7:06 pm
Posts: 8
I am struggling with the very thing as we speak. As you said, updates on the child are not working. From the log output I see that Hibernate is trying to update the child with a statement like

update CONTACT set CONTACT_NAME=?, ...PERSON_ID=? where CONTACT_TYPE=?

The "where" clause specifies only the child key, but does not include the parent key. This I think is a problem with using the documented one-to-many technique, with keys having business meaning, since I do not believe that sequential keys would cause a problem for such an update statement. In my case, CONTACT_TYPE is not unique among children with different parents.

I also created a composite-id version of the scenario I am trying to model, and briefly went back to that, to see if Hibernate will manage the associations. So far I have not found a formula that works.


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.