简体   繁体   English

org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新

[英]org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

I get below mentioned stack trace though the data gets inserted successfully. 虽然成功插入了数据,但我得到了下面提到的堆栈跟踪。

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

Please note: 请注意:

a) My db has no records currently b) Data gets inserted into DB successfully. a)我的数据库当前没有记录b)数据成功插入数据库。

Here I am trying to persist an Event object which contains two Attendee Objects. 在这里,我试图坚持一个包含两个参加者对象的Event对象。 That is all. 就这些。

My Test Class: 我的测试班:

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: 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.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>

Your Event.hbm.xml says: 您的Event.hbm.xml说:

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

In plain english, this means that the column Attendee.attendeeId is the foreign key for the association attendees and points to the primary key of Event . 简单来说,这意味着Attendee.attendeeId列是关联attendees外键 ,并指向Event的主键。

When you add those Attendees to the event, hibernate updates the foreign key to express the changed association. 将这些参加者添加到事件时,hibernate会更新外键以表示更改的关联。 Since that same column is also the primary key of Attendee, this violates the primary key constraint. 由于同一列也是参加者的主键,因此违反了主键约束。

Since an Attendee's identity and event participation are independent, you should use separate columns for the primary and foreign key. 由于参加者的身份和事件参与是独立的,因此您应该为主键和外键使用单独的列。

Edit: The selects might be because you don't appear to have a version property configured, making it impossible for hibernate to know whether the attendees already exists in the database (they might have been loaded in a previous session), so hibernate emits selects to check. 编辑:选择可能是因为您似乎没有配置版本属性,使得hibernate无法知道参与者是否已经存在于数据库中(它们可能已在先前的会话中加载),因此hibernate发出选择去检查。 As for the update statements, it was probably easier to implement that way. 至于更新语句,实现这种方式可能更容易。 If you want to get rid of these separate updates, I recommend mapping the association from both ends, and declare the Event -end as inverse . 如果你想摆脱这些单独的更新,我建议从两端映射关联,并将Event -end声明为inverse

You can find your sample code completely here: http://www.java2s.com/Code/Java/Hibernate/OneToManyMappingbasedonSet.htm 您可以在此处完整地找到示例代码: http//www.java2s.com/Code/Java/Hibernate/OneToManyMappingbasedonSet.htm

Have a look and check the differences. 看看并检查差异。 specially the even_id in : 特别是even_id:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.ConstraintViolationException: 无法执行 JDBC 批量更新 - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 休眠一对一映射错误org.hibernate.exception.ConstraintViolationException:无法执行JDBC批更新 - Hibernate one to one mapping error org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 无法执行JDBC批处理更新:线程“ main” org.hibernate.exception.ConstraintViolationException中的异常: - Could not execute JDBC batch update:Exception in thread “main” org.hibernate.exception.ConstraintViolationException: org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新[由于唯一约束] - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update [Due to Unique Constraint] org.hibernate.exception.ConstraintViolationException:保存高级OneToMany替换项时无法执行JDBC批更新 - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at saving hibrnate OneToMany reletionship JPA + Hibernate:org.hibernate.exception.ConstraintViolationException:无法执行批处理 - JPA + Hibernate : org.hibernate.exception.ConstraintViolationException: could not execute batch org.hibernate.exception.ConstraintViolationException:无法执行语句 - org.hibernate.exception.ConstraintViolationException: could not execute statement org.hibernate.exception.ConstraintViolationException:无法执行语句 - org.hibernate.exception.ConstraintViolationException: could not execute statement 线程“main” org.hibernate.exception.ConstraintViolationException 中的异常:无法执行语句 - Exception in thread “main” org.hibernate.exception.ConstraintViolationException: could not execute statement 无法执行语句; SQL [不适用]; 约束 [null]; 嵌套异常是 org.hibernate.exception.ConstraintViolationException: - could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM