我有以下Hibernate配置:
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3336/db</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
和以下映射文件:
<hibernate-mapping default-lazy="false">
<class name="Table" table="TABLE" batch-size="10" dynamic-update="true">
<id name="id" column="ID">
<generator class="uuid"/>
</id>
<property name="name" column="NAME" not-null="true"/>
</class>
</hibernate-mapping>
如果执行此操作,它可以正常工作:
sessionFactory.getSession().beginTransaction();
sessionFactory.getSession().save(new Table());
sessionFactory.getSession().save(new Table());
sessionFactory.getSession().save(new Table());
sessionFactory.getSession().save(new Table());
sessionFactory.getSession().getTransaction().commit();
但是,将rewriteBatchedStatements=true
添加到hibernate.connection.url
:
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3336/db?rewriteBatchedStatements=true</prop>
在提交时抛出以下异常:
org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 4; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:95)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
任何想法有什么不对?
答案 0 :(得分:2)
我使用的是具有此错误的MySQL Connector / J 5.1.28: http://bugs.mysql.com/bug.php?id=68562。降级到5.1.24解决了这个问题。
答案 1 :(得分:2)
我也遇到了这个问题,并通过将mysql连接器更新到5.1.36来修复它:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>