在sql server表上设置IDENTITY_INSERT时出错

时间:2015-03-11 16:14:41

标签: java sql-server hibernate jpa

正如我在本文JPA SET IDENTITY_INSERT not working中提到的那样,我成功地使用了

SET IDENTITY_INSERT

查询我的目的。但是今天在我设法配置连接池后,我遇到了这种类型的错误。

java.sql.SQLException: IDENTITY_INSERT is already ON for table 'mydb.dbo.[table1]'. Cannot perform SET operation for table 'dbo.[table2]'.
2015-03-11 15:52:36,126 WARN  org.hibernate.util.JDBCExceptionReporter.logExceptions:233 - SQL Error: 544, SQLState: 23000
2015-03-11 15:52:36,127 ERROR org.hibernate.util.JDBCExceptionReporter.logExceptions:234 - Cannot insert explicit value for identity column in table '[table2]' when IDENTITY_INSERT is set to OFF.
2015-03-11 15:52:36,128 ERROR   org.hibernate.event.def.AbstractFlushingEventListener.performExecutions:324 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at     org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)

有人可以提出解决此错误的建议吗?

1 个答案:

答案 0 :(得分:0)

使用Identity Insert是一个对称命令,打开它后,您必须将其关闭。例如。

Set Identity_Insert ErrorLog On

Insert  Into ErrorLog
        ( LineNumber, ErrorMessage )
Values  ( 0, 'Test 1' ) ,
        ( 1, 'Test 2' )

Set Identity_Insert ErrorLog Off

希望有所帮助。