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!