2011-04-27 89 views
5

時,重複鍵'PRIMARY'的條目'1'當我第一次插入表時(在創建和刪除我的數據庫之後),我收到錯誤鍵'PRIMARY'的重複條目'1' 。這會導致回滾。如果我再次執行行插入確定。任何人都可以向我解釋爲什麼/我做錯了什麼?休眠在第一次插入表

我有一個用戶類(預填充在數據庫中),它有一個附加到它的任務列表。該數據庫擁有表的用戶,用戶的任務,任務

User.hbm包含

<id name="id" type="java.lang.Integer"> 
      <column name="id" /> 
      <generator class="increment" /> 
     </id> 
<list name="tasks" cascade="all" table="user_tasks" lazy="false"> 
      <key column="user_id"/> 
      <index column="id"/> 
      <many-to-many column="task_id" class="com.some.package.classes.Task"/> 
     </list>` 

Task.hbm包含

<id name="id" type="java.lang.Integer"> 
     <column name="id" /> 
     <generator class="increment" /> 
    </id> 
<list name="users" cascade="all" table="user_tasks" lazy="false" 
    inverse="false"> 
     <key column="task_id"/> 
     <index column="id"/> 
     <many-to-many column="user_id" class="com.some.packageclasses.User"/> 
    </list> 

的2個日誌顯示(不工作,然後工作)

r:1112 - Inserting collection: [com.some.package.classes.User.tasks#1] 
14:39:16,488 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 
14:39:16,489 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?)  
14:39:16,489 TRACE AbstractBatcher:513 - preparing statement 
14:39:16,489 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:16,489 TRACE IntegerType:151 - binding '0' to parameter: 2 
14:39:16,490 TRACE IntegerType:151 - binding '1' to parameter: 3 
14:39:16,490 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:16,490 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:16,490 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:16,490 TRACE IntegerType:151 - binding '1' to parameter: 2 
14:39:16,490 TRACE IntegerType:151 - binding '2' to parameter: 3 
14:39:16,490 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:16,490 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:16,490 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:16,491 TRACE IntegerType:151 - binding '2' to parameter: 2 
14:39:16,491 TRACE IntegerType:151 - binding '3' to parameter: 3 
14:39:16,491 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:16,491 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:16,491 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:16,491 TRACE IntegerType:151 - binding '3' to parameter: 2 
14:39:16,491 TRACE IntegerType:151 - binding '4' to parameter: 3 
14:39:16,491 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:16,491 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:16,492 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:16,492 TRACE IntegerType:151 - binding '4' to parameter: 2 
14:39:16,492 TRACE IntegerType:151 - binding '5' to parameter: 3 
14:39:16,492 DEBUG AbstractCollectionPersister:1194 - done inserting collection: 5 rows inserted 
14:39:16,493 DEBUG AbstractBatcher:66 - Executing batch size: 5 
14:39:16,510 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 
14:39:16,511 TRACE AbstractBatcher:562 - closing statement 
14:39:16,515 DEBUG JDBCExceptionReporter:92 - Could not execute JDBC batch update [insert into user_tasks (user_id, id, task_id) values (?, ?, ?)] 
java.sql.BatchUpdateException: Duplicate entry '1' for key 'PRIMARY' 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028) 
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) 
    at com.some.package.TimeMgmt.submitTasks(TimeMgmt.java:165) 
    at com.some.package.TimeMgmt.main(TimeMgmt.java:228) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407) 
    at com.mysql.jdbc.Util.getInstance(Util.java:382) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976) 
    ... 12 more 
14:39:16,518 WARN JDBCExceptionReporter:100 - SQL Error: 1062, SQLState: 23000 
14:39:16,518 ERROR JDBCExceptionReporter:101 - Duplicate entry '1' for key 'PRIMARY' 
14:39:16,519 ERROR AbstractFlushingEventListener: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:94) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028) 
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) 
    at com.some.package.TimeMgmt.submitTasks(TimeMgmt.java:165) 
    at com.some.package.TimeMgmt.main(TimeMgmt.java:228) 
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 'PRIMARY' 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
    ... 9 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407) 
    at com.mysql.jdbc.Util.getInstance(Util.java:382) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976) 
    ... 12 more 
14:39:16,519 TRACE ConnectionManager:501 - registering flush end 
14:39:16,519 DEBUG JDBCTransaction:186 - rollback 
14:39:16,521 DEBUG JDBCTransaction:197 - rolled back JDBC Connection 
14:39:16,522 TRACE JDBCContext:242 - after transaction completion 
14:39:16,522 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 
14:39:16,522 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 
14:39:16,522 TRACE DriverManagerConnectionProvider:152 - returning connection to pool, pool size: 1 
14:39:16,522 TRACE SessionImpl:450 - after transaction completion 
14:39:16,525 TRACE SessionImpl:301 - closing session 
14:39:16,526 TRACE ConnectionManager:398 - connection already null in cleanup : no action 
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028) 
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) 
    at com.some.package.TimeMgmt.submitTasks(TimeMgmt.java:165) 
    at com.some.package.TimeMgmt.main(TimeMgmt.java:228) 
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 'PRIMARY' 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
    ... 9 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407) 
    at com.mysql.jdbc.Util.getInstance(Util.java:382) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976) 
    ... 12 more 



>14:39:40,542 DEBUG AbstractCollectionPersister:1112 - Inserting collection: [com.some.package.classes.User.tasks#1] 
14:39:40,542 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 
14:39:40,543 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:40,543 TRACE AbstractBatcher:513 - preparing statement 
14:39:40,544 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:40,544 TRACE IntegerType:151 - binding '0' to parameter: 2 
14:39:40,544 TRACE IntegerType:151 - binding '1' to parameter: 3 
14:39:40,544 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:40,544 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:40,544 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:40,545 TRACE IntegerType:151 - binding '1' to parameter: 2 
14:39:40,545 TRACE IntegerType:151 - binding '2' to parameter: 3 
14:39:40,545 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:40,545 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:40,545 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:40,545 TRACE IntegerType:151 - binding '2' to parameter: 2 
14:39:40,546 TRACE IntegerType:151 - binding '3' to parameter: 3 
14:39:40,546 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:40,546 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:40,546 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:40,546 TRACE IntegerType:151 - binding '3' to parameter: 2 
14:39:40,546 TRACE IntegerType:151 - binding '4' to parameter: 3 
14:39:40,546 DEBUG AbstractBatcher:248 - reusing prepared statement 
14:39:40,547 DEBUG SQL:111 - insert into user_tasks (user_id, id, task_id) values (?, ?, ?) 
14:39:40,547 TRACE IntegerType:151 - binding '1' to parameter: 1 
14:39:40,548 TRACE IntegerType:151 - binding '4' to parameter: 2 
14:39:40,548 TRACE IntegerType:151 - binding '5' to parameter: 3 
14:39:40,548 DEBUG AbstractCollectionPersister:1194 - done inserting collection: 5 rows inserted 
14:39:40,548 DEBUG AbstractBatcher:66 - Executing batch size: 5 
14:39:40,550 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 
14:39:40,550 TRACE AbstractBatcher:562 - closing statement 
14:39:40,551 TRACE ConnectionManager:501 - registering flush end 
14:39:40,551 TRACE AbstractFlushingEventListener:344 - post flush 
14:39:40,551 TRACE JDBCContext:228 - before transaction completion 
14:39:40,551 TRACE SessionImpl:421 - before transaction completion 
14:39:40,559 DEBUG JDBCTransaction:147 - committed JDBC Connection 
14:39:40,559 TRACE JDBCContext:242 - after transaction completion 
14:39:40,559 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 
14:39:40,560 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 
14:39:40,560 TRACE DriverManagerConnectionProvider:152 - returning connection to pool, pool size: 1 
14:39:40,560 TRACE SessionImpl:450 - after transaction completion 
14:39:40,560 TRACE SessionImpl:301 - closing session 
+1

只是好奇知道。爲什麼選擇使用XML配置而不是註釋?或者這是一個遺留項目? – 2011-04-27 14:43:55

+0

我對此很新。我用MyEclipse來生成休眠層。 – Chris 2011-04-27 15:19:57

回答

3

Increment絕對不好,如果你有多個進程寫入相同的ta你一定會碰到碰撞。

所以用< generator class="identity"/>試試。

+0

恐怕我已經嘗試了本機和身份認證,但都在第一次嘗試插入時產生相同的錯誤。 – Chris 2011-04-27 15:21:15

1

這已經有一段時間,因爲我用HBM文件,但它不應該是:

<id name="id"> 
     <column name="id" /> 
     <generator class="native" /> 
    </id> 

+0

恐怕我已經嘗試了本機和身份認證,但都在第一次嘗試插入時產生相同的錯誤。 – Chris 2011-04-27 15:20:59

+0

我已經與mysql結合使用本機,並從未有過這個問題。我也會建議使用JPA註釋i.o. hbm文件。看[這裏](http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-entity)瞭解更多信息。 – 2011-04-27 15:23:57

+0

我會給它一個爆炸。乾杯。 – Chris 2011-04-27 15:33:26

0

我在轉儲和恢復MySQL數據庫後出現同樣的錯誤。重新啓動應用程序服務器(在我的情況下是Tomcat)解決了這個問題。