Cannot add or update a child row: a foreign key constraint fails

ningjingzhiyuan2012 2012-08-30
我用mysql做的关系数据库,直接操作数据是可以插入数据的。但是我用hibernate插入数据时出现了错误:Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
我的主表:
   CREATE TABLE DataSet (        
id               VARCHAR(30)       NOT NULL      ,
Dname         VARCHAR(30) NOT NULL        PRIMARY KEY,
descr            VARCHAR(30)        NOT NULL,
foreign  key(id)  references  IED(id)  on  delete  cascade  on  update cascade
)ENGINE=InnoDB DEFAULT CHARSET=gb2312;
从表:
CREATE TABLE Tag (        
Dname     VARCHAR(30)    DEFAULT 'aaa',
address    INT(4),
name            VARCHAR(30)       NOT NULL      PRIMARY KEY,
dataType     VARCHAR(30)       NOT NULL,
unit            VARCHAR(30) ,
mode     VARCHAR(30)       NOT NULL,
descr     VARCHAR(30),
coefficient   FLOAT(5,2),
KEY  Dname(Dname),
constraint tag_ibfk_1 foreign key(Dname) references dataset(Dname)  on  delete  cascade  on  update cascade
)ENGINE=InnoDB DEFAULT CHARSET=gb2312;
Tag.hbm.xml:
<?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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.bright.scada.model.Tag" table="tag" catalog="scadaserver">
        <id name="name" type="java.lang.String">
            <column name="name" length="30" />
            <generator class="assigned" />
        </id>
        <many-to-one name="dataSet"  class="com.bright.scada.model.DataSet" fetch="select"  insert="false" update="false">
            <column name="Dname" length="30" not-null="true" />
        </many-to-one>
        <property name="address" type="java.lang.Integer">
            <column name="address" />
        </property>
        <property name="dataType" type="java.lang.String">
            <column name="dataType" length="30" not-null="true" />
        </property>
        <property name="unit" type="java.lang.String">
            <column name="unit" length="30" />
        </property>
        <property name="mode" type="java.lang.String">
            <column name="mode" length="30" not-null="true" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="descr" length="30" />
        </property>
        <property name="coefficient" type="java.lang.Float">
            <column name="coefficient" precision="5" />
        </property>
    </class>
</hibernate-mapping>
DataSet.hbm.xml:
<?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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.bright.scada.model.DataSet" table="dataset" catalog="scadaserver">
        <id name="name" type="java.lang.String">
            <column name="Dname" length="30" />
            <generator class="assigned" />
        </id>
        <many-to-one name="ied" class="com.bright.scada.model.IED" fetch="select">
            <column name="id" length="30" not-null="true" />
        </many-to-one>
        <property name="description" type="java.lang.String">
            <column name="descr" length="30" not-null="true" />
        </property>
        <set name="tags" inverse="true" cascade="all">
            <key>
                <column name="Dname" length="30" not-null="true" />  <!--指定外键字段名-->
            </key>
            <one-to-many class="com.bright.scada.model.Tag"/>
           
         
        </set>
    </class>
</hibernate-mapping>
测试类:
Configuration config=new Configuration().configure();     SessionFactory sessionFactory = config.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx = session.beginTransaction();

      DataSet dataSet1=new DataSet();
         Set<Tag> tags=new HashSet<Tag>();//
         dataSet1.setTags(tags);//
         dataSet1.setName("Anlog");
         dataSet1.setDescription("模拟量");
         dataSets.add(dataSet1);
         dataSet1.setIed(ied1);
         session.save(dataSet1);
        
        Tag tag1=new Tag();
        tag1.setName("F_Ua");
        tag1.setDataType("float");
        tag1.setUnit("Hz");
        tag1.setMode("R");
        tag1.setDescription("线路频率");
        tag1.setAddress(103);
        tag1.setCoefficient(0.01f);
        tags.add(tag1);
        tag1.setDataSet(dataSet1);
        session.save(tag1);
        
   
        
         tx.commit();
    session.close();
错误:Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`scadaserver`.`tag`, CONSTRAINT `tag_ibfk_1` FOREIGN KEY (`Dname`) REFERENCES `dataset` (`Dname`) ON DELETE CASCADE ON UPDATE CASCADE)
Global site tag (gtag.js) - Google Analytics