Results 1 to 2 of 2
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

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

    Guys,
    i am new to java. trying to store some data into 2 separate tables in DB.(customer and task).
    Task has a FK (customer ID).I can store data into customer table, but gets error when trying to store into task table.

    "1828 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails (`john`.`task`, CONSTRAINT `id` FOREIGN KEY (`id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)"

    i am using hibernate file to do the job.

    These are my files.

    hibernate config file
    --------------------

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

    <hibernate-configuration>
    <session-factory>
    <property name = "hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
    <property name = "show_sql">true</property>
    <property name="hibernate.connection.driver_class">com.mysql .jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/john</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="Customer.hbm.xml"/>
    <mapping resource="Task.hbm.xml"/>


    </session-factory>

    </hibernate-configuration>


    Customer xml file
    -----------------

    <?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 = "domain">
    <class name="Customer" table="customer">
    <id name="id" column="id" type="java.lang.Integer" >
    <generator class="increment"/>


    </id>
    <property name="name" column="name" type="java.lang.String" ></property>
    </class>
    </hibernate-mapping>



    task xml file
    -------------
    <?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 = "domain">
    <class name="Task" table="task">
    <id name="idtask" column="idtask" type="java.lang.Integer" >
    <generator class="increment"/>


    </id>
    <property name="taskDescription" column="taskDescription" type="java.lang.String" ></property>
    <property name="date" column="date" type="java.lang.String" ></property>
    <property name="time" column="time" type="java.lang.String" ></property>
    <property name="id" column="id" type="java.lang.Integer" ></property>


    </class>
    </hibernate-mapping>

    Customer table has 2 columns --- id and name

    Task table has 5 columns --- idtask, taskDescription, date, time, id (FK to customer)

  2. #2
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    6

    Default

    hi
    please check whether you are referencing `customer` table 's id field please check the FOREIGN KEY constrain

Similar Threads

  1. foreign keys in jtable gui
    By blackpearlmoni in forum New To Java
    Replies: 0
    Last Post: 10-31-2009, 05:52 PM
  2. how to remove constraint from output file
    By mazu in forum New To Java
    Replies: 18
    Last Post: 10-08-2009, 09:32 AM
  3. innodb and foreign keys problem
    By Pierced1 in forum JDBC
    Replies: 0
    Last Post: 06-29-2009, 03:59 AM
  4. Cannot add or update a child row....
    By Pierced1 in forum JDBC
    Replies: 2
    Last Post: 06-29-2009, 03:32 AM
  5. System.getProperty with foreign language
    By barakz in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 05:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •