Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: version 3.2.4.sp1, May 18, 2007
Mapping documents:
hibernate.hbm.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">gary</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mvchurch</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">mvchurch</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/visualbuilder/hibernate/User.hbm.xml"/>
<mapping resource="com/visualbuilder/hibernate/PhoneNumber.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.visualbuilder.hibernate">
<class name="User" table="USERS">
<id name="userId" type="java.lang.Long" column="user_id">
<generator class="increment"></generator>
</id>
<property name="firstName" type="java.lang.String" column="firstName" length="20"/>
<property name="lastName" type="java.lang.String" column="lastName" length="20"/>
<property name="age" type="java.lang.Integer" column="age" length="-1"/>
<property name="email" type="java.lang.String" column="email" length="40" />
<set name="phoneNumbers" cascade="all">
<key column="user_id" />
<one-to-many class="com.visualbuilder.hibernate.PhoneNumber"/>
</set>
</class>
</hibernate-mapping>
PhoneNumber.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.visualbuilder.hibernate">
<class name="PhoneNumber" table="PhoneNumber">
<composite-id>
<key-property column="user_id" type="java.lang.Long" name="userId" />
<key-property column="numberType" type="java.lang.String" name="numberType" />
</composite-id>
<property type="java.lang.Long" name="phone" >
<column name="phone" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
package com.visualbuilder.hibernate.client;
import java.util.List;
import com.visualbuilder.hibernate.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* @author glmarsh
*
*/
public class UserManager {
private SessionFactory factory = null;
private Session session = null;
public UserManager() {
factory =new Configuration().configure().buildSessionFactory();
session = factory.openSession();
}
public SessionFactory getSessionFactory()
{
return factory;
}
public User getUser(long userId) {
session = factory.openSession();
User user = (User)session.get(User.class, new Long(userId));
session.close();
return user;
}
public void saveUser(User user){
if( !session.isOpen()) session = factory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
}
.
.
.
}
Full stack trace of any exception that occurs:
org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 7810893; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:71)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.visualbuilder.hibernate.client.UserManager.saveUser(UserManager.java:46)
at com.visualbuilder.hibernate.client.JUnitTestClient.testSaveUser(JUnitTestClient.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:
MySQL Version 5.1
The generated SQL (show_sql=true):
Hibernate: select max(user_id) from mvchurch.USERS
Hibernate: insert into mvchurch.USERS (firstName, lastName, age, email, user_id) values (?, ?, ?, ?, ?)
The Problem :
As you can see the problem appears to be an error in the row count as returned by executing the session.save(user); call in UserManager. When I step through using the debugger the save() seems to work and shows I would have saved out one record but when I call session.close() I get the error thrown as shown above. I haven't been able to find this error listed anywhere in on the Web except as listed in the api. Has anyone experienced this or does anyone know what is the possible cause?
Regards,
glmarsh