Hi,
I have started working with a set of stock data, and unable to update an existing record in database. I am working with basic hibernate 5.x update on an existing record, but it's failing due to unique constraint set in MySQL database for two columns (composite).
public void updateTkshst(Integer tkshstID, int open) { Session session = factory.openSession(); Transaction tx = null; Tkshst tkshst = new Tkshst(); try { tx = session.beginTransaction(); tkshst = (Tkshst) session.get(Tkshst.class, tkshstID); tkshst.setOpen(open); session.update(tkshst); session.flush(); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }
Error log is: Hibernate: select max(ID) from tkshst_tb Hibernate: insert into sankhyadb.tkshst_tb (TICKER, DATE, OPEN, HIGH, LOW, CLOSE, VOLUME, ADJCLOSE, CREATE_TIMESTAMP, ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Oct 27, 2016 10:50:38 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO: HHH000010: On release of batch it still contained JDBC statements Oct 27, 2016 10:50:38 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1062, SQLState: 23000 Oct 27, 2016 10:50:38 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Duplicate entry 'ABB-20161027' for key 'tkdate' Oct 27, 2016 10:50:38 AM org.hibernate.engine.jdbc.batch.internal.BatchingBatch performExecution ERROR: HHH000315: Exception executing batch [could not execute batch] Oct 27, 2016 10:50:38 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute batch] Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute batch at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1411) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:475) at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3168) at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2382) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68) at com.ty.fintech.setups.hibernate.TkshstCRUDSetup.addTkshst(TkshstCRUDSetup.java:60) at com.ty.fintech.setups.hibernate.TkshstCRUDSetup.main(TkshstCRUDSetup.java:29) Caused by: org.hibernate.exception.ConstraintViolationException: could not execute batch at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:119) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:97) at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:147) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:206) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:611) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1405) ... 10 more Caused by: java.sql.BatchUpdateException: Duplicate entry 'ABB-20161027' for key 'tkdate' at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1981) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1393) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:110) ... 18 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'ABB-20161027' for key 'tkdate' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.Util.getInstance(Util.java:384) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334) at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1933) ... 20 more
|