Hibernate:无法添加或更新子行:外键约束失败

时间:2013-05-27 03:55:13

标签: java mysql hibernate hibernate-mapping struts-1

我尝试通过hibernate

在Mysql DB中插入值
Hibernate: insert into EMPLOYEEM (FIRSTNAME, LASTNAME) values (?, ?)
Hibernate: insert into MEETING (MEETING_DATE, SUBJECT) values (?, ?)
Hibernate: insert into MEETING (MEETING_DATE, SUBJECT) values (?, ?)
Hibernate: insert into EMPLOYEEM (FIRSTNAME, LASTNAME) values (?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID) values (?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID) values (?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID) values (?, ?)
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:1007)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:354)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at com.atom.javah3M2M.Main.main(Main.java:34)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`test`.`employee_meeting`, CONSTRAINT `FK_EMPLOYEE` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`employee_id`))
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2054)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
    ... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`employee_meeting`, CONSTRAINT `FK_EMPLOYEE` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`employee_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2006)
    ... 11 more

表的结构如下

表employeeem

===============

  

employee_id,名字,姓氏

     

employee_id bigint(10)PK

     

firstname varchar(50)

     

lastname varchar(50)


表会议

=============

  

meeting_id,subject,meeting_date

     

meeting_id bigint(20)PK

     

subject varchar(50)

     

meeting_date日期


表employee_meeting

======================

  

id,employee_id,meeting_id

     

id int(11)PK

     

employee_id bigint(20)

     

meeting_id bigint(20)


Employee.hbm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.atom.javah3M2M">


<class name="com.atom.javah3M2M.Employeem" table="EMPLOYEEM">
    <id name="empid" column="EMPLOYEE_ID">
        <generator class="native" />
    </id>

    <property name="fname" column ="FIRSTNAME"/>
    <property name="lname" column="LASTNAME" />

    <set name="meetings" table="EMPLOYEE_MEETING"
        inverse="false" lazy="true" fetch="select" cascade="save-update">
        <key column="EMPLOYEE_ID" not-null="true" />

        <many-to-many entity-name="com.atom.javah3M2M.Meeting">
            <column name="MEETING_ID" not-null="true" /> 
        </many-to-many>
    </set>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.atom.javah3M2M">


<class name="com.atom.javah3M2M.Meeting" table="MEETING">

    <id name="meetingId" type="java.lang.Long" column="MEETING_ID">
        <generator class="native" />
    </id>

    <property name="meetingDate" type="date" column="MEETING_DATE"/>
    <property name="subject" column="SUBJECT" />

  <set name="employees" table="EMPLOYEE_MEETING"
        inverse="true" lazy="true" fetch="select">
        <key column="MEETING_ID" />

         <many-to-many entity-name="com.atom.javah3M2M.Employeem">
            <column name="EMPLOYEE_ID" not-null="true" /> 
        </many-to-many>
    </set>
</class>
</hibernate-mapping>

package com.atom.javah3M2M;


import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;






public class Main {


public static void main(String[] args) {

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();


    Meeting meeting1 = new Meeting("Quaterly Sales meeting",new Date(System.currentTimeMillis()));
    Meeting meeting2 = new Meeting("Weekly Status meeting",new Date(System.currentTimeMillis()));

    Employeem employee1 = new Employeem("Sergey", "Brin");
    Employeem employee2 = new Employeem("Larry", "Page");

    employee1.getMeetings().add(meeting1);
    employee1.getMeetings().add(meeting2);
    employee2.getMeetings().add(meeting1);

    session.save(employee1);
    session.save(employee2);

    session.getTransaction().commit();
    session.close();
}
}


0 个答案:

没有答案