-->
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: Batch update row count wrong: 0
PostPosted: Wed Sep 29, 2004 9:34 am 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Hibernate version: 2.1.6

Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="User"
        table="User"
        dynamic-update="false"
        dynamic-insert="false"
    >
        <id
            name="username"
            column="username"
            type="java.lang.String"
            length="32"
            unsaved-value="null"
        >
            <generator class="assigned">
            </generator>
        </id>
        <many-to-one
            name="company"
            class="Company"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="id"
        />
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class
        name="Company"
        table="Company"
        dynamic-update="false"
        dynamic-insert="false"
    >
        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="native">
            </generator>
        </id>
        <bag
            name="users"
            lazy="true"
            inverse="true"
            cascade="save-update"
        >
              <key
                  column="id"
              >
              </key>
              <one-to-many
                  class="User"
              />
        </bag>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
    [junit] ERROR [main] SessionImpl.execute(2379) | Could not synchronize database state with session
    [junit] net.sf.hibernate.HibernateException: Batch update row count wrong: 0
    [junit] at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
    [junit] at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
    [junit] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
    [junit] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
    [junit] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
    [junit] at UserDAOTest.tearDown(UserDAOTest.java:50)
    [junit] at junit.framework.TestCase.runBare(TestCase.java:130)
    [junit] at junit.framework.TestResult$1.protect(TestResult.java:106)
    [junit] at junit.framework.TestResult.runProtected(TestResult.java:124)
    [junit] at junit.framework.TestResult.run(TestResult.java:109)
    [junit] at junit.framework.TestCase.run(TestCase.java:118)
    [junit] at junit.framework.TestSuite.runTest(TestSuite.java:208)
    [junit] at junit.framework.TestSuite.run(TestSuite.java:203)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:289)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:656)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:558)
    [junit] Testsuite: UserDAOTest
    [junit] Tests run: 6, Failures: 0, Errors: 1, Time elapsed: 0.265 sec
    [junit] Testcase: testSaveUser(UserDAOTest):   Caused an ERROR
    [junit] Batch update row count wrong: 0
    [junit] net.sf.hibernate.HibernateException: Batch update row count wrong: 0
    [junit] at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
    [junit] at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
    [junit] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
    [junit] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
    [junit] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
    [junit] at UserDAOTest.tearDown(UserDAOTest.java:50)
    [junit] Test UserDAOTest FAILED


Name and version of the database you are using: MySQL 4.0.21

I have problem implementing the Company<->User associations. 1 Company can have many Users. 1 User can belong to 0 or 1 Company. Can someone please tell me what I have done wrong and why I am keep getting the "Batch update row count wrong: 0" error?

Here are the java classes:

Code:
   public class Company {
      private Long id;
      private List users = new ArrayList();

      // ... Getters/Setters ...

      // Additional class to add user to company
      public void addUser(User user) {
         user.setCompany(this);
         users.add(user);
      }
   }

   public class User {
      private String username;
      private Company company;

      // ... Getters/Setters ...
   }

   public class UserDAOTest {
      private UserDAO userDAO = null;
      
      public void testSaveUser() throws Exception {
         Company company = new Company();

         User user = new User();
         user.setUsername("junit");

         company.addUser(user);
         userDAO.saveCompany(company);

         assertNotNull(company.getId());
      }
   }

   public class UserDAO {
      public Company saveCompany(Company company) throws DAOException {
         int count =
            ((Integer) getHibernateTemplate()
               .find("select count(*) from Company c where c.id=?", company.getId()).iterator()
               .next()).intValue();
         
         if (count == 0) {
            getHibernateTemplate().save(company);
         } else {
            getHibernateTemplate().saveOrUpdate(company);
         }

         return company;
      }
   }


Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 6:45 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Can someone please help me, I am stucked at this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 7:08 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Hi

I think I know what I am doing wrong and I know what I need to do to fix it. The problem is I don't know how to do it. :)

As you can see in my Hibernate mapping for the User class, I use username as the primary key and since I can't use unsaved-value attribute, the getHibernateTemplate().saveOrUpdate() function doesn't know whether the record is new or an existing record.

To solve this, I need to use the version property for the User class or use Interceptor.isUnSaved.

I tried the version property by defining the following things in the User class:

/**
*@hibernate.class table="User"
*/
public class User {
private String username;
private Company company;
private int version;

// Getters/Setters for username and company go here and their Hibernate tags.

/**
*@hibernate.vesion column="version" unsaved-value="null"
*/
public int getVersion() {
return this.version;
}

public void setVersion(int version) {
this.version = version;
}
}

When I tried to generate the mapping file, there was a validation error saying unsaved-value attribute must be declared for version property, which I clearly did.

I tried to use Interceptor.isUnSaved with the following abstract class but nothing changed. How do I use it?

import net.sf.hibernate.Interceptor;

public abstract class NullInterceptor implements Interceptor {
public Boolean isUnsaved(Object entity) {
return null;
}
}


Regards,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 7:22 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
The supplied DTD hibernate-mapping_2_0.dtd in the xdoclet.modules.hibernate.resources package does not have the 'unsaved-value' attribute for the version element. I will try the
patched hibernate-mapping_2_0.dtd file.

How do I use Interceptor.isUnSaved()? I already checked out the Hibernate FAQ and its documentation but still unable to use it.

Thanks


Top
 Profile  
 
 Post subject: I have similar issue
PostPosted: Fri Jan 07, 2005 2:42 pm 
Newbie

Joined: Wed Dec 22, 2004 4:41 pm
Posts: 2
Hi,

I have similar issue. Were you ever able to figure out how to fix this ...

Would appreciate any help !!

Amit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 11, 2005 5:58 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Have you tried the latest version of XDoclet? Or find the patch for hibernate-mapping_2_0.dtd and use it.

I think I fixed the problem by using the patched DTD.

HTH


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.