-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Parent Child foreign key constraint fails
PostPosted: Sun Jun 19, 2005 10:05 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

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.PostingIdentification"
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.PostingIdentification"
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(277) | Could not synchronize datab
ase state with session
[junit] org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
[junit] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
[junit] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
[junit] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener
.java:274)
[junit] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[junit] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
[junit] at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:249)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:313)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:612)
[junit] at org.sabah.dao.hibernate.PostingDAOHibernate.savePostingIdentification(PostingDAOHibernate.java:2
6)
[junit] at org.sabah.dao.PostingDAOTest.testAddPosting(PostingDAOTest.java:175)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.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.java: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:208)
[junit] at junit.framework.TestSuite.run(TestSuite.java:203)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:289)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:656)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.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.executeBatch(ServerPreparedStatement.java:822)
[junit] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
[junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.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.DataIntegrityViolationException: (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.executeBatch(ServerPreparedStatement.java:822)
[junit] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
[junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
[junit] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener
.java:274)
[junit] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[junit] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
[junit] at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:249)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:313)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:612)
[junit] at org.sabah.dao.hibernate.PostingDAOHibernate.savePostingIdentification(PostingDAOHibernate.java:2
6)
[junit] at org.sabah.dao.PostingDAOTest.testAddPosting(PostingDAOTest.java:175)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 5:13 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:16 am
Posts: 44
Location: Bangalore
Hi,
Try with cascade="save-update,persist".
:-)

_________________
Please vote if my Postings helps. :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.