-->
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.  [ 6 posts ] 
Author Message
 Post subject: Updating when it should be inserting
PostPosted: Thu Feb 12, 2004 6:56 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
I have a class Assignment, whose id is a long with unsaved-value of 0. I explicitly set the id to 0 when a new class is created. However, when I saveOrUpdate a new Assignment, is tries to update a row, and then returns a "Batch update row count wrong: 0" error.

Mapping:
Code:
<hibernate-mapping package="com.martin.model" >
    <class name="Assignment" table="assignments">
       
        <id name="id" column="id" type="long" unsaved-value="0">
         <generator class="sequence">
                <param name="sequence">assignments_id_seq</param>
           </generator>
        </id>

      <property name="sequenceNumber" type="int" column="sequence_number" not-null="false" unique="false" />
      <property name="minimumScore" type="java.lang.Integer" column="minimum_score" not-null="false" unique="false" />
      <property name="quickStudy" type="boolean" column="quick_study" not-null="true" unique="false" />
      <property name="master" type="boolean" column="master" not-null="true" unique="false" />
      <property name="test" type="boolean" column="test" not-null="true" unique="false" />

      <many-to-one name="course" class="Course" column="course_id" />

      <set name="units" table="assignment_unit_mapping" lazy="true">
         <key column="assignment_id"/>
         <many-to-many column="unit_id" class="Unit"/>
      </set>

      <!--inverse="true" means that Homework is responsible for the relation-->
       <set name="homework" inverse="true" lazy="true">
          <key column="assignment_id"/>
          <one-to-many class="Homework"/>
      </set>
   </class>


Save Code:
Code:
      assignment.setUnits(unitSet);
      assignment.setQuickStudy( quickStudy );
      assignment.setMaster(master);
      assignment.setTest(test);
      course = (Course)get("course");
      assignment.setCourse(course);
      log.info("-  saving assignment: "+assignment);
      assignmentDAO.saveOrUpdate(assignment);
      log.info("- Assignment saved.");



I am using an interceptor with Webwork, which closes and flushes the session after the view is rendered, so that code is not here.
Any help? I have searched the forums and documentation, and am still confused as to why the unsaved-value=0 is not respected. If I print out the id of the new Assignment before saving it, it is 0, so I know it should get inserted, not updated.

The Error:
Code:
Hibernate: update assignments set sequence_number=?, minimum_score=?, quick_study=?, master=?, test=?, course_id=? where id=?
Feb 12, 2004 5:48:45 PM net.sf.hibernate.impl.SessionImpl execute
SEVERE: Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 6:59 pm 
Newbie

Joined: Tue Feb 10, 2004 12:03 am
Posts: 16
refere this link

http://www.hibernate.org/74.html#A27


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 7:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show us more of your calling code ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 11:45 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Thank you for your responses.
Whole saving method:
Code:
   public String go() throws HibernateException {
      String result = INPUT;
      
      AssignmentDAO assignmentDAO = new AssignmentDAO();
      assignmentDAO.initialize(getSession());
      assignment = (Assignment)get("assignment");

      getSession().update(assignment);
      assignment.setMinimumScore( this.minimumScore );
      
      UnitDAO unitDAO = new UnitDAO();
      unitDAO.initialize(getSession());
      List units = unitDAO.findByIds( unitIds );
      Set unitSet = new HashSet();
      unitSet.addAll(units);
      
      assignment.setUnits(unitSet);
      assignment.setQuickStudy( quickStudy );
      assignment.setMaster(master);
      assignment.setTest(test);

      course = (Course)get("course");
      assignment.setCourse(course);
      log.info("-  saving assignment: "+assignment);
      assignmentDAO.saveOrUpdate(assignment);
      log.info("- Assignment saved.");
      result = SUCCESS;
      return result;
   }

As you can see, the assignment is retrieved from the session - get("assignment"). The code where it is placed into the session (HTTPsession, not Hibernate session):
Code:
   public String go() throws HibernateException {
      assignment = new Assignment();
      course = (Course)get("course");
      assignment.setCourse(course);
          assignment.setSequenceNumber(course.getAssignments().size() + 1);
      set("assignment", assignment);
      return SUCCESS;
   }

The code called in AssignmentDAO:
Code:
   public void saveOrUpdate( Object obj ) throws HibernateException {
      Session s = null;
      s = getSession();
      saveOrUpdate( obj, s);
   }

Thanks to the previous submitter for their reference to the FAQ, but I assure you I have already read it and the thread it came form. Indeed, it was reading that which lead me to post, since my situation regarding this error seemed to differ from theirs. The difference is that my unsaved-value is set to 0 for id, and the class sets the id to 0 when a new one is created, yet Hibernate updates instead of inserts. Somehow my object is not considered new, and therein my troubles began...

thanks!
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 4:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do it again with batch_size=0 and have a look at the debug logs

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 10:25 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
I seem to have solved it, although to be honest I could not tell you how. The main change is that I circumvented my DAOs and callled getSession().saveOrUpdate(assignment) directly in the class. Somehow, the problem is in there. So much for layering.
Thanks to all, I wish I could be more specific about how this got solved.
-Michael


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