将Plat Framework 1.2.7升级到1.5.2之后,MySQLIntegrityConstraintViolationException

时间:2018-11-12 09:02:46

标签: mysql hibernate playframework

由于Java和服务器上的发行版更新,我需要更新Play Framework 1应用程序。

对JDK 1.8进行必要的更改后,一切似乎都可以正常工作。但是,插入新记录时,出现以下错误。

08:49:44,922 ERROR ~ Error during device update (Job): org.hibernate.exception.ConstraintViolationException: could not execute statement
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1460)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1440)
    at play.db.jpa.JPABase._save(JPABase.java:58)
    at play.db.jpa.GenericModel.create(GenericModel.java:375)
    at models.Device.createDevice(Device.java:154)
    at logic.helpers.DevicePerformer.doJobWithResult(DevicePerformer.java:104)
    at logic.helpers.DevicePerformer.doJobWithResult(DevicePerformer.java:1)
    at play.jobs.Job$2.apply(Job.java:224)
    at play.db.jpa.JPA.withTransaction(JPA.java:285)
    at play.db.jpa.JPA.withinFilter(JPA.java:238)
    at play.db.jpa.JPAPlugin$TransactionalFilter.withinFilter(JPAPlugin.java:304)
    at play.jobs.Job.withinFilter(Job.java:201)
    at play.jobs.Job.call(Job.java:220)
    at play.jobs.Job$1.call(Job.java:135)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3171)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3686)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:380)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
    ... 19 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '215' for key 'PRIMARY'
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
    ... 27 more

MySQL告诉我,下一个索引是44806,这正是我所期望的。

我有两个服务器,并且仅运行旧代码(播放1.2.7)时,它可以正常工作,并按预期提供了一个ID。两台服务器使用相同的数据库。

这是一个新的实现,它提供了较低的值(在下面的示例中为215),因此一定不能使用数据库中的值,而是其他东西。我确实注意到了一个名为hibernate_sequence的表,带有一个'next_val'字段,该字段确实给出了一个值接近215。它有25行,其中每一列都是相同的数字。

新的Hibernate实现是否进行了某些更改,所以我需要更改某些内容才能再次使用“下一个自动索引”?

1 个答案:

答案 0 :(得分:0)

区别在于,在Hibernate 5中未指定生成策略的情况下,Hibernate会使用其自己的策略。

使用MySQL值可以通过以下注释完成:

@GeneratedValue(strategy = GenerationType.IDENTITY)