My problem is that an unsuccessful update follows the code below.
EntityManager manager=JpaUtil.getEntityManager();
EntityTransaction trx= manager.getTransaction();
trx.begin();
TypedQuery<Tipi> query = manager.createQuery("from Tipi",Tipi.class);
List<Tipi> registros= query.getResultList();
for (int i = 0; i < 500; i++) {
registros.get(i).setCod(i+1);
if(i % 20 ==0){
manager.flush();
manager.clear();
}
}
trx.commit();
manager.close();
JpaUtil.closeFactory();
So as you can see I'm trying to update the cod
field by inserting these
values with the loop however when I execute the above code the result is that only the first record is updated the others remain empty, if I take out if
it drops the following exception
:
Caused by: javax.persistence.PersistenceException: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [4]; actual row count: 3; expected: 1 at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82) ... 1 more Caused by: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [4]; actual row count: 3; expected: 1 at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:89) at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.checkRowCounts(BatchingBatch.java:151) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:128) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:97) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3281) at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525) at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) ... 1 more
In my persistence.xml
the batch size is 20, thanks for any help.