web-dev-qa-db-ja.com

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

注意:

a)現在データベースにレコードがありませんb)データがDBに正常に挿入されます。

ここでは、2つの参加者オブジェクトを含む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>
5
Bashu

Event.hbm.xmlには次のように書かれています。

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

平易な英語では、これは、列Attendee.attendeeIdが関連付けattendeesの-​​外部キーであり、Eventの主キーを指していることを意味します。

これらの参加者をイベントに追加すると、hibernateは変更された関連付けを表すために外部キーを更新します。同じ列がAttendeeの主キーでもあるため、これは主キーの制約に違反します。

参加者のIDとイベントへの参加は独立しているため、主キーと外部キーに別々の列を使用する必要があります。

編集:selectは、バージョンプロパティが構成されていないように見えるため、参加者がデータベースにすでに存在するかどうかをhibernateが認識できないため(前のセッションで読み込まれた可能性がある)、hibernateがselectを発行することが原因である可能性があります。チェックする。更新ステートメントに関しては、おそらくその方法で実装する方が簡単でした。これらの個別の更新を削除する場合は、両端から関連付けをマッピングし、Event- endをinverseとして宣言することをお勧めします。

14

サンプルコードはここで完全に見つけることができます: http://www.Java2s.com/Code/Java/Hibernate/OneToManyMappingbasedonSet.htm

見て、違いを確認してください。特に:のeven_id

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