-->
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: Hibernate collection saving issue
PostPosted: Thu Apr 20, 2006 5:39 pm 
Newbie

Joined: Wed Oct 19, 2005 7:29 am
Posts: 5
Hi, The issue is as follows. I have a list of contacts associated with a supplier class. In database, it is mirrored by the contact table having supplier_id as foreign key. The add supplier works fine. But, when I try to update the supplier in a way that the list of contacts contains One contact to be be edited (it has an id). ANd one contact without id, there is a problem.
Hibernate inserts a new contact as expected, and updates the contact with id, but the supplier_id belonging to all other contacts in the database is set to zero. It must be sthign trivial !!!

Hibernate version:
3.1
Mapping documents:

<class name="SupplierDTO" table="supplier">
<set name="supplierContacts" cascade="all">
<key>
<column name="supplier_id"></column>
</key>
<one-to-many class="SupplierContactDTO" />
</set>
</class>

Code between sessionFactory.openSession() and session.close():
Session session = HibernateSession.getSessionFactory().openSession();
tx = session.beginTransaction();
session.saveOrUpdate(supplierObj);
session.flush();
tx.commit();
session.close();
Full stack trace of any exception that occurs:
Database corruped
Name and version of the database you are using:
mysql 5.0
The generated SQL (show_sql=true):
[junit] 2006-04-20 10:02:15,845 DEBUG [org.hibernate.SQL] insert into supplier_contacts (title, first_name, middle_name, last_name, phone_number, fax_number, email_address, language_id, contact_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
[junit] 2006-04-20 10:02:15,861 DEBUG [org.hibernate.SQL] update supplier set supplier_name=?, address_id=?, relationship_id=?, supplier_website=? where supplier_id=?
[junit] 2006-04-20 10:02:15,874 DEBUG [org.hibernate.SQL] update supplier_contacts set title=?, first_name=?, middle_name=?, last_name=?, phone_number=?, fax_number=?, email_address=?, language_id=? where contact_id=?
[junit] 2006-04-20 10:02:15,880 DEBUG [org.hibernate.SQL] update supplier_contacts set supplier_id=null where supplier_id=?
[junit] 2006-04-20 10:02:15,884 DEBUG [org.hibernate.SQL] update supplier_contacts set supplier_id=? where contact_id=?
[junit] 2006-04-20 10:02:15,887 DEBUG [org.hibernate.SQL] update supplier_contacts set supplier_id=? where contact_id=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 6:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You'll need to post the buggy mapping, which is the Contact one. Its relationship to supplier looks screwy. Please use code tags when posting it, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 7:04 pm 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
Your mapping documents seem somewhat sparse - where are the definitions for your identifier fields, and what are the unsaved values for your identifier fields ?

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 1:18 am 
Newbie

Joined: Wed Oct 19, 2005 7:29 am
Posts: 5
here is the contact mapping.
Code:
   <class name="SupplierContactDTO" table="supplier_contacts">
      <id name="contactId" column="contact_id">
         <generator class="increment" />
      </id>
   </class>



-thanks


Top
 Profile  
 
 Post subject: Resolved...
PostPosted: Fri Apr 21, 2006 2:43 am 
Newbie

Joined: Wed Oct 19, 2005 7:29 am
Posts: 5
Its resolved now. Was missing a not-null="true" constraint.
Code:
         <set name="supplierContacts"  cascade="all">
         <key column="supplier_id" not-null="true" />
         <one-to-many class="SupplierContactDTO" />
      </set>


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.