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.  [ 2 posts ] 
Author Message
 Post subject: Batch update returned unexpected row count from update
PostPosted: Thu Jun 19, 2008 11:43 am 
Newbie

Joined: Wed Jun 18, 2008 8:28 pm
Posts: 3
Hello,

I have a problem using Hibernate. I have two classes, one is Client, the other is Hobby. Every client can have several hobbies. The data is read in from an XML file. Im trying to use session.saveOrUpdate(client); But i get this error. My database at the moment is empty, I want the code to insert if the object does not exist in database and update if it does, the ID can be the clients name, for hobby, the hobby name. My mapping:
client.hbm.xml
Code:
<class name="Client" table="clients">
      <id name="Name" type="string" column="cl_id" unsaved-value="0">
      <generator class="assigned"/>
     </id>
     <property name="Name">
       <column name="name" />
     </property>
     <property name="Address">
       <column name="address" />
     </property>
     <property name="DateOfBirth">
       <column name="dob" />
     </property>
      <set name="hobbies" table="cl_hobby" cascade="save-update,persist">
         <key column="cl_id"/>
         <many-to-many column="hobby_id" class="Hobby"/>
      </set>
   </class>



hobby.hbm.xml

Code:
<class name="Hobby" table="hobbies">
      <id name="Name" type="string" column="hobby_id" unsaved-value="0">
            <generator class="assigned"/>
        </id>
      <property name="Name">
         <column name="name" />
      </property>
   </class>


My code for main:

Code:
Mapping mapping = new Mapping();
      Configuration cfg = new Configuration();
      cfg.configure();
      SessionFactory sessFactory = cfg.buildSessionFactory();
      try {
          mapping.loadMapping("book-mapping.xml");
          FileReader reader = new FileReader("book.xml");
          Unmarshaller unmarshaller = new Unmarshaller(Clients.class);
          unmarshaller.setMapping(mapping);

          Clients book = (Clients)unmarshaller.unmarshal(reader);
          List clients = book.getClients();
          Iterator iter = clients.iterator();
          while ( iter.hasNext() ) {
             Client person = (Client) iter.next();try{
                   Session session = sessFactory.openSession();
                   Transaction tx = session.beginTransaction();
                   session.saveOrUpdate(person);                   
                   tx.commit();
                   session.close();
   
                } catch(Exception e) {
                   e.printStackTrace();
                }
          }
       } catch (Exception e) {
          
          System.err.println(e.getMessage());
          e.printStackTrace(System.err);
       }


The Hobby and Client has it's setters and getters, for name's and everything else. Any ideas?Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:53 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
If the id is a unique id, then the saveOrUpdate method of the HibernateSession should work as you expect it. It will recognize common names, and only update those records for common people/hobbies.

What is the actual problem you are experiencing?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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