SHARE
TWEET

Untitled

a guest Jul 20th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EntityManager manager=JpaUtil.getEntityManager();
  2.         EntityTransaction trx= manager.getTransaction();
  3.         trx.begin();
  4.         TypedQuery<Tipi> query = manager.createQuery("from Tipi",Tipi.class);
  5.         List<Tipi> registros= query.getResultList();
  6.  
  7.  
  8.         for (int i = 0; i < 500; i++) {
  9.             registros.get(i).setCod(i+1);
  10.             if(i % 20 ==0){
  11.                 manager.flush();
  12.                 manager.clear();
  13.             }
  14.         }
  15.  
  16.         trx.commit();
  17.         manager.close();
  18.         JpaUtil.closeFactory();
  19.      
  20. Caused by: javax.persistence.PersistenceException: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [4]; actual row count: 3; expected: 1
  21.     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
  22.     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
  23.     at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
  24.     ... 1 more
  25. Caused by: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [4]; actual row count: 3; expected: 1
  26.     at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:89)
  27.     at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
  28.     at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.checkRowCounts(BatchingBatch.java:151)
  29.     at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:128)
  30.     at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:97)
  31.     at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3281)
  32.     at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
  33.     at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
  34.     at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158)
  35.     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
  36.     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
  37.     at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
  38.     at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
  39.     at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
  40.     at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421)
  41.     at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
  42.     at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
  43.     at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
  44.     ... 1 more
  45.      
  46. public void saveOrUpdate(T obj) {
  47.     EntityManager manager = ConnectionFactory.getEntityManager();
  48.     try {
  49.         manager.getTransaction().begin();
  50.         if (obj.getId() == null) {
  51.             manager.persist(obj);
  52.         } else {
  53.             manager.merge(obj);
  54.         }
  55.         manager.getTransaction().commit();
  56.     } catch (Exception e) {
  57.         manager.getTransaction().rollback();
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top