Hibernate does not update information

Asked

Viewed 346 times

1

My problem if it comes to an update unsuccessfully 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 field cod inserting these values with the loop but when I execute the above code the result is that only the first record is updated the others remain empty, if I take the if he releases 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 batch size is 20, thanks for any help.

1 answer

0

In contrast to the loop, if it is convenient, I think you should do something like below (without looping to return list of objects):

public void saveOrUpdate(T obj) {
    EntityManager manager = ConnectionFactory.getEntityManager();
    try {
        manager.getTransaction().begin();
        if (obj.getId() == null) {
            manager.persist(obj);
        } else {
            manager.merge(obj);
        }
        manager.getTransaction().commit();
    } catch (Exception e) {
        manager.getTransaction().rollback();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.