Announcement Announcement Module
Collapse
No announcement yet.
Parent Child foreign key constraint fails Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parent Child foreign key constraint fails

    Hibernate version:3.0.5

    Mapping documents:
    <?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
    >
    <class
    name="org.sabah.model.Posting.CategorizedItem"
    table="CATEGORIZED_ITEM"
    lazy="true"
    >

    <composite-id
    name="id"
    class="org.sabah.model.Posting.CategorizedItem$Id"
    unsaved-value="any"
    >
    <key-property
    name="categoryId"
    type="java.lang.Long"
    column="CATEGORY_ID"
    length="16"
    access="field"
    />

    <key-property
    name="postId"
    type="java.lang.Long"
    column="POST_ID"
    length="16"
    access="field"
    />

    </composite-id>

    <property
    name="userId"
    type="java.lang.String"
    update="false"
    insert="true"
    column="USER_ID"
    not-null="true"
    />

    <property
    name="dateAdded"
    type="java.util.Date"
    update="false"
    insert="true"
    column="DATE_ADDED"
    not-null="true"
    />

    <many-to-one
    name="category"
    class="org.sabah.model.Posting.Category"
    cascade="none"
    outer-join="auto"
    update="false"
    insert="false"
    column="CATEGORY_ID"
    not-null="true"
    />

    <many-to-one
    name="postingIdentification"
    class="org.sabah.model.Posting.PostingIdentificati on"
    cascade="none"
    outer-join="auto"
    update="false"
    insert="false"
    column="POST_ID"
    not-null="true"
    />

    <!--
    To add non XDoclet property mappings, create a file named
    hibernate-properties-CategorizedItem.xml
    containing the additional properties and place it in your merge dir.
    -->

    </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
    >
    <class
    name="org.sabah.model.Posting.PostingIdentificatio n"
    table="PostingIdentification"
    >
    <cache usage="transactional" />

    <id
    name="id"
    type="long"
    unsaved-value="null"
    >
    <column
    name="ID"
    sql-type="BIGINT(15)"
    />
    <generator class="assign">
    <!--
    To add non XDoclet generator parameters, create a file named
    hibernate-generator-params-PostingIdentification.xml
    containing the additional parameters and place it in your merge dir.
    -->
    </generator>
    </id>

    <discriminator
    column="discriminator"
    />

    </set>

    <set
    name="categorizedItems"
    lazy="true"
    inverse="true"
    cascade="all-delete-orphan"
    sort="unsorted"
    >

    <key foreign-key="FK2_CATEGORIZED_POST_ID">
    <column name="POST_ID" not-null="true" length="16"/>
    </key>

    <one-to-many
    class="org.sabah.model.Posting.CategorizedItem"
    />

    </set>


    <!--
    To add non XDoclet property mappings, create a file named
    hibernate-properties-PostingIdentification.xml
    containing the additional properties and place it in your merge dir.
    -->
    <subclass
    name="org.sabah.model.Posting.ClassifiedProperties "
    >

    <!--
    To add non XDoclet property mappings, create a file named
    hibernate-properties-ClassifiedProperties.xml
    containing the additional properties and place it in your merge dir.
    -->

    </subclass>

    </class>

    </hibernate-mapping>


    Code between sessionFactory.openSession() and session.close():

    getHibernateTemplate().saveOrUpdate(pid);

    Full stack trace of any exception that occurs:

    [junit] [stufftolet] WARN [main] JDBCExceptionReporter.logExceptions(71) | SQL Error: 1216, SQLState: 23000
    [junit] [stufftolet] ERROR [main] JDBCExceptionReporter.logExceptions(72) | Cannot add or update a child row: a fore
    ign key constraint fails
    [junit] [stufftolet] ERROR [main] AbstractFlushingEventListener.performExecutions(27 7) | Could not synchronize datab
    ase state with session
    [junit] org.hibernate.exception.ConstraintViolationExcepti on: Could not execute JDBC batch update
    [junit] at org.hibernate.exception.ErrorCodeConverter.convert (ErrorCodeConverter.java:74)
    [junit] at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
    [junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:181)
    [junit] at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:226)
    [junit] at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:136)
    [junit] at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r
    .java:274)
    [junit] at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
    [junit] at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:730)
    [junit] at org.springframework.orm.hibernate3.HibernateAccess or.flushIfNecessary(HibernateAccessor.java:249)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:313)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:612)
    [junit] at org.sabah.dao.hibernate.PostingDAOHibernate.savePo stingIdentification(PostingDAOHibernate.java:2
    6)
    [junit] at org.sabah.dao.PostingDAOTest.testAddPosting(Postin gDAOTest.java:175)
    [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    [junit] at java.lang.reflect.Method.invoke(Method.java:585)
    [junit] at junit.framework.TestCase.runTest(TestCase.java:154 )
    [junit] at junit.framework.TestCase.runBare(TestCase.java:127 )
    [junit] at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    [junit] at junit.framework.TestResult.runProtected(TestResult .java:124)
    [junit] at junit.framework.TestResult.run(TestResult.java:109 )
    [junit] at junit.framework.TestCase.run(TestCase.java:118)
    [junit] at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    [junit] at junit.framework.TestSuite.run(TestSuite.java:203)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.run(JUnitTestRunner.java:289)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.launch(JUnitTestRunner.java:656)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.main(JUnitTestRunner.java:558)
    [junit] Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
    [junit] at com.mysql.jdbc.ServerPreparedStatement.executeBatc h(ServerPreparedStatement.java:822)
    [junit] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:57)
    [junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:174)
    [junit] ... 25 more
    [junit] Testsuite: org.sabah.dao.PostingDAOTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.132 sec

    [junit] Testcase: testAddPosting(org.sabah.dao.PostingDAOTest): Caused an ERROR
    [junit] (Hibernate operation: Could not execute JDBC batch update): data integrity violated by SQL 'insert into CATE
    GORIZED_ITEM (USER_ID, DATE_ADDED, CATEGORY_ID, POST_ID) values (?, ?, ?, ?)'; nested exception is java.sql.BatchUpdateE
    xception: Cannot add or update a child row: a foreign key constraint fails
    [junit] org.springframework.dao.DataIntegrityViolationExce ption: (Hibernate operation: Could not execute JDBC batch
    update): data integrity violated by SQL 'insert into CATEGORIZED_ITEM (USER_ID, DATE_ADDED, CATEGORY_ID, POST_ID) values
    (?, ?, ?, ?)'; nested exception is java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key const
    raint fails
    [junit] java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
    [junit] at com.mysql.jdbc.ServerPreparedStatement.executeBatc h(ServerPreparedStatement.java:822)
    [junit] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:57)
    [junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:174)
    [junit] at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:226)
    [junit] at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:136)
    [junit] at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r
    .java:274)
    [junit] at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
    [junit] at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:730)
    [junit] at org.springframework.orm.hibernate3.HibernateAccess or.flushIfNecessary(HibernateAccessor.java:249)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:313)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:612)
    [junit] at org.sabah.dao.hibernate.PostingDAOHibernate.savePo stingIdentification(PostingDAOHibernate.java:2
    6)
    [junit] at org.sabah.dao.PostingDAOTest.testAddPosting(Postin gDAOTest.java:175)
    [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)


    [junit] Test org.sabah.dao.PostingDAOTest FAILED

    Name and version of the database you are using:MySql 4.1.11

    The generated SQL (show_sql=true):

    insert into CATEGORIZED_ITEM (USER_ID, DATE_ADDED, CATEGORY_ID, POST_ID) values
    (?, ?, ?, ?)'; nested exception is java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key const
    raint fails

    Hi guys,

    I got a real big problem here. The CategorizedItem.Id.postId is depends on PostingIdentification.Id. But when trying to save it, the database always complaint the above msg. I thought Hibernate will try to save the 'parent' before saving the 'child' so that they won't be any foreign key constraint fails exception.
    Pls help, Thanks !

    regards,
    prettyhandling

  • #2
    Mark,

    I am having the same problem, I dont know it is caused by MySQL 5 or Hibernate,

    Did you find any solution for this.

    Appreciate your help

    Thanks

    Comment

    Working...
    X