MySQL5.0.15
一个问题表(question),一个回答表(answer)分别如下:
create table question
(
   qid                            int             auto_increment               not null,
   title                          VARCHAR(50),
   content                        text,
   itemid                         int,
   subid                          int,
   userid                         VARCHAR(50),
   grade                          VARCHAR(50),
   offerscore                     int,
   status                         int,
   questiontime                   datetime,
   clickcount                     int,
   acceptflag                     int,
   commenflag                     int,
   primary key (qid)
);
create table answer
(
   aid                            int            auto_increment         not null,
   quesans                        VARCHAR(50),
   userid                         VARCHAR(50),
   grade                          VARCHAR(50),
   anstime                        datetime,
   status                         int,
   qid                            int,
   primary key (aid) ,
   foreign key (qid) references question(qid) on delete cascade 
);question表对应地hibernate3.1的映射文件为:Question.hbm.xml,内容如下:
<hibernate-mapping>
    <class name="com.iwtxokhtd.vo.Question" table="question">
        <id name="qid" type="java.lang.Integer">
            <column name="qid" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" length="50" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" length="65535" />
        </property>
        <property name="itemid" type="java.lang.Integer">
            <column name="itemid" />
        </property>
        <property name="subid" type="java.lang.Integer">
            <column name="subid" />
        </property>
        <property name="userid" type="java.lang.String">
            <column name="userid" length="50" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="grade" length="50" />
        </property>
        <property name="offerscore" type="java.lang.Integer">
            <column name="offerscore" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="status" />
        </property>
        <property name="questiontime" type="java.util.Date">
            <column name="questiontime" length="19" />
        </property>
        <property name="clickcount" type="java.lang.Integer">
            <column name="clickcount" />
        </property>
        <property name="acceptflag" type="java.lang.Integer">
            <column name="acceptflag" />
        </property>
        <property name="commenflag" type="java.lang.Integer">
            <column name="commenflag" />
        </property>
        <set name="answers" inverse="true" table="answer">
            <key>
                <column name="qid" />
            </key>
            <one-to-many class="org.lxh.myzngt.vo.Answer" />
        </set>
    </class>
</hibernate-mapping>
answer表对应地hibernate3.1的映射文件为:Answer.hbm.xml,内容如下:
<hibernate-mapping>
    <class name="com.iwtxokhtd.vo.Answer" table="answer">
        <id name="aid" type="java.lang.Integer">
            <column name="aid" />
            <generator class="native" />
        </id>
        <many-to-one name="question" class="org.lxh.myzngt.vo.Question"
         fetch="select">
         <column name="qid" />
        </many-to-one>
        <property name="quesans" type="java.lang.String">
            <column name="quesans" length="50" />
        </property>
        <property name="userid" type="java.lang.String">
            <column name="userid" length="50" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="grade" length="50" />
        </property>
        <property name="anstime" type="java.util.Date">
            <column name="anstime" length="19" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="status" />
        </property>
    </class>
</hibernate-mapping>

解决方案 »

  1.   

     Cannot add or update a child row: a foreign key constraint fails (`myznt/answer`, CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`qid` 你试图INSERT或UPDATE一个在question不存在的qid到answer表。建议检查一下程序。[align=center]====  ====
    [/align]
      

  2.   

    此时回答问题能成功,进入个人中心中也能查到自己所提的问题,但查不到自己所回答和被采纳了的问题。
    这是查询问题数量的HQL语句:String hql="select count(q.qid) from Question as q WHERE q.qid IN (SELECT a.qid FROM Answer AS a WHERE a.userid=?)";
    这是查询已被采纳的问题数量的HQL语句:String hql="select count(q.qid) from Question as q WHERE q.qid IN (SELECT a.qid FROM Answer AS a WHERE a.userid=? AND status=?)";
    这是列出已被采纳的问题列表的HQL语句:String hql="from Question as q WHERE q.qid IN (SELECT a.qid FROM Answer AS a WHERE a.userid=? AND status=?)";
    这是列出已回答了的问题列表的HQL语句:String hql="from Question as q WHERE q.qid IN (SELECT a.qid FROM Answer AS a WHERE a.userid=?)";
    如果在保持Question.hbm.xml和Answer.hbm.xml文件不变的情况下,使用查询已回答的问题以及已被采纳的问题时就会出现can't resolve property 'qid' in上面的HQL语句异常。
    如果在Answer.hbm.xml后面加上:  <property name="qid" type="java.lang.Integer"> 
                 <column name="qid" /> 
             </property> 并把改成<many-to-one name="question" class="com.iwtxokhtd.vo.Question" 
             fetch="select"> <many-to-one name="question" class="com.iwtxokhtd.vo.Question" 
             insert="false" update="false">时, 查询已回答的问题以及已被采纳的问题可以正常使用,但这时回答问题却不成功,就会出现开始给的异常。
      

  3.   

    question中是有qid的,用到了这个qid的程序已经给在上面了,就是这个问题解决不了,望赐教!
      

  4.   

    这种情况,一般应该是question中的qid不存在导致的,你确认你要插入的answer记录的qid存在吗?对于严格mysql,如果插入某条记录时检查到foreign key中指定的值在该外键所在的表中不存在的话,是不允许插入的。
      

  5.   

    我也遇到过这样的问题,我弄了半天发现是MySQL 的可视化工具里面的对应表的外键默认为不级联,我改成级联之后就可以啦,你也可以试试。
      

  6.   

    我也遇到这个问题,Cannot add or update a child row: a foreign key constraint fails一般第一次映射出了问题,第二次你又重新映射,出现了foreign key的状态不一致(因为你第一次已经映射了,外键也已经存在了,特别会出现在连表名都改变了,),把数据库中的表全部删除,重新再映射,这时就不会出现这种情况了,总结一下;对于这种报底层的错误,其实没有多少好的办法,最好的方法,换一个干净的环境,看看还会出错吗。
      

  7.   

    還有一種情況就是在你的po裏面 有屬性沒有set或get方法(即 原來是 integer 生成getter,setter方法後又將integer 改為int類型的 )
      

  8.   

    检查一下表的外键设置,注意 表的大小写问题, 在使用 mysqldump 时 外键的表名可能会变成小写,仔细检查一下,重建外键即可.