2010-08-29 86 views
4

雖然數據插入成功,但我得到了下面提到的堆棧跟蹤。org.hibernate.exception.ConstraintViolationException:無法執行JDBC批量更新

Hibernate: select attendee_.attendeeId, attendee_.attendeeName as attendee2_1_ from attendee attendee_ where attendee_.attendeeId=? 
Hibernate: select attendee_.attendeeId, attendee_.attendeeName as attendee2_1_ from attendee attendee_ where attendee_.attendeeId=? 
Hibernate: insert into event (eventName, startDate, eventId) values (?, ?, ?) 
Hibernate: insert into attendee (attendeeName, attendeeId) values (?, ?) 
Hibernate: insert into attendee (attendeeName, attendeeId) values (?, ?) 
Hibernate: update attendee set attendeeId=? where attendeeId=? 
Hibernate: update attendee set attendeeId=? where attendeeId=? 
Aug 29, 2010 7:39:10 PM org.hibernate.util.JDBCExceptionReporter logExceptions 
WARNING: SQL Error: 1062, SQLState: 23000 
Aug 29, 2010 7:39:10 PM org.hibernate.util.JDBCExceptionReporter logExceptions 
SEVERE: Duplicate entry '11' for key 'PRIMARY' 
Aug 29, 2010 7:39:10 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions 
SEVERE: 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:69) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980) 
    at com.practice.hibernate.basic.BasicOperations.main(BasicOperations.java:51) 
Caused by: java.sql.BatchUpdateException: Duplicate entry '11' for key 'PRIMARY' 
    at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195) 
    ... 6 more 
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980) 
    at com.practice.hibernate.basic.BasicOperations.main(BasicOperations.java:51) 
Caused by: java.sql.BatchUpdateException: Duplicate entry '11' for key 'PRIMARY' 
    at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195) 
    ... 6 more 

請注意:

一)我的數據庫沒有記錄目前 b)數據被插入到數據庫成功。

這裏我試圖堅持一個包含兩個Attendee對象的Event對象。就這些。

我的測試類:

public static void main(String[] args) { 
    Session session = HibernateRuntime.getSession(); 

    try { 
     Set<Attendee> attendees = new HashSet<Attendee>(2); 

     Attendee attendee = new Attendee(); 
     attendee.setAttendeeId(3); 
     attendee.setAttendeeName("Baswanth Rao"); 

     Attendee attendee1 = new Attendee(); 
     attendee1.setAttendeeId(4); 
     attendee1.setAttendeeName("Razi Ahmed"); 

     attendees.add(attendee); 
     attendees.add(attendee1); 

     Event event = new Event(); 
     event.setEventId(11); 
     event.setEventName("Initiatives Workshop 3"); 
     event.setStartDate(new Date()); 
     event.setAttendees(attendees); 

     session.save(event); 
     session.flush(); 

    } finally { 
     session.close(); 
    } 
} 

Event.hbm.xml:

<hibernate-mapping package="com.practice.hibernate.vo"> 
    <class name="Event" table="event"> 
     <id name="eventId" column="eventId" type="long"> 
      <generator class="assigned" /> 
     </id> 

     <property name="eventName" type="string" length="100" /> 
     <property name="startDate" type="date" /> 

     <set name="attendees" cascade="all"> 
      <key column="attendeeId" /> 
      <one-to-many class="Attendee" /> 
     </set> 
    </class> 
</hibernate-mapping> 

的hibernate.cfg.xml

<hibernate-configuration> 
    <session-factory> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost/test</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 
     <property name="connection.autocommit">false</property> 

     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="show_sql">true</property> 

     <mapping resource="com/practice/hibernate/vo/Event.hbm.xml"></mapping> 
     <mapping resource="com/practice/hibernate/vo/Attendee.hbm.xml"></mapping> 
    </session-factory> 
</hibernate-configuration> 
+0

刪除依賴於與會者並再次嘗試。或者仔細檢查你的Attendee.hbm.xml – mhshams 2010-08-29 14:23:48

+0

你確定測試代碼只運行一次嗎? 你可以在你的主要方法中放一些日誌來確保它只執行一個itme。 – mhshams 2010-08-29 14:57:31

+0

Bashu,使用「編輯」鏈接將此添加到問題中。它位於問題文本的左下角,位於休眠標籤按鈕的下方。 – meriton 2010-08-29 14:58:07

回答

11

你連t.hbm.xml說:

<set name="attendees" cascade="all"> 
    <key column="attendeeId" /> 
    <one-to-many class="Attendee" /> 
</set> 

在簡單的英語,這意味着列Attendee.attendeeId是爲協會attendees和點的Event主鍵外鍵

當您將這些與會者添加到事件中時,hibernate將更新外鍵以表示更改的關聯。由於同一列也是參加者的主鍵,因此違反了主鍵約束。

由於參與者的身份和事件參與是獨立的,因此您應該爲主鍵和外鍵使用單獨的列。

編輯:選擇可能是因爲您沒有配置版本屬性,這使得hibernate無法知道數據庫中是否已存在與會者(它們可能已在前一會話中加載過),所以休眠發出選擇來檢查。至於更新聲明,實現這種方式可能更容易。如果你想擺脫這些單獨的更新,我建議從兩端映射關聯,並聲明Event - 結束爲inverse

+0

是的,那是問題,你應該在你的參與者表中有另一列 – mhshams 2010-08-29 15:06:01

+0

是啊......謝謝Meriton。 這個問題現在已經解決......我很抱歉,這是一個基本問題,我應該徹底驗證它。 – Bashu 2010-08-29 16:04:40

+0

你是否也可以解釋爲什麼前2個選擇和最後2個更新被執行。我們能否避免這兩個更新語句? – Bashu 2010-08-29 16:24:46

-2

您可能需要處理javax.persistence.RollbackException

相關問題