-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: A foreign key constraint fails
PostPosted: Mon Oct 27, 2003 3:38 pm 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Hi,

I have a two level class hierarchy, with the following mapping:

<class
name="DocsElement"
table="DocsElement">

<id
name="chvp"
column="chvp"
type="integer">
<generator class="increment"/>
</id>

<joined-subclass
name="DocsFolder"
table="DocsFolder">
<key column="subChvp"/>

<joined-subclass
name="FolderEscala"
table="FolderEscala">
<key column="subChvp"/>
</joined-subclass>

</joined-subclass>
</class>

When I try to store a DocsFolder everything goes OK, but when I try to store a FolderEscala, i get the following error:

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


What am i doing wrong?

Thanks in advance,
Joao Rangel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 3:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you please show us the full stacktrace and Hibernate in debug mode ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: A foreign key constraint fails
PostPosted: Tue Oct 28, 2003 6:43 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Here is the exception stack trace.

Thanks again,
Joao Rangel

-------------------------------------------------------------------------------------

C:\j2sdk1.4.2\bin\javaw.exe -client

Oct 28, 2003 10:30:39 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

WARNING: SQL Error: 1216, SQLState: S1000

Oct 28, 2003 10:30:39 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

SEVERE: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"

Oct 28, 2003 10:30:39 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

WARNING: SQL Error: 1216, SQLState: S1000

Oct 28, 2003 10:30:39 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

SEVERE: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"

Oct 28, 2003 10:30:39 AM net.sf.hibernate.JDBCException <init>

SEVERE: could not insert: [FolderEscala#11]

java.sql.SQLException: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)

at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1700)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1569)

at com.mchange.v2.c3p0.impl.C3P0PreparedStatement.executeUpdate(C3P0PreparedStatement.java:68)

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:517)

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:489)

at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:23)

at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2313)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2267)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2211)

at StorageDockImpl.commit(StorageDockImpl.java:71)

at DocsConsole.main(DocsConsole.java:44)

Oct 28, 2003 10:30:39 AM net.sf.hibernate.impl.SessionImpl execute

SEVERE: Could not synchronize database state with session

java.sql.SQLException: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)

at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1700)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1569)

at com.mchange.v2.c3p0.impl.C3P0PreparedStatement.executeUpdate(C3P0PreparedStatement.java:68)

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:517)

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:489)

at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:23)

at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2313)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2267)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2211)

at StorageDockImpl.commit(StorageDockImpl.java:71)

at DocsConsole.main(DocsConsole.java:44)

rethrown as net.sf.hibernate.JDBCException: could not insert: [FolderEscala#11]: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:531)

at net.sf.hibernate.persister.NormalizedEntityPersister.insert(NormalizedEntityPersister.java:489)

at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:23)

at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2313)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2267)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2211)

at StorageDockImpl.commit(StorageDockImpl.java:71)

at DocsConsole.main(DocsConsole.java:44)

(continues)

Debuggee process terminated.
Process exited with exit code 0.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 7:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show me the log output in DEBUG level of Hibernate trying to insert new FolderEscala

FolderEscala fe = new FolderEscala();
// fill new folder escala
session.save(fe);
session.flush();

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hibernate debug level
PostPosted: Tue Oct 28, 2003 7:41 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Hi,

I'm not understanding what do you mean with hibernate debug level? Could you help me how to do that?

Joao Rangel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 7:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the Hibernate documentation, from top to bottom before posting here again. It covers logging and many other topics.

TIA.


Top
 Profile  
 
 Post subject: Debug
PostPosted: Tue Oct 28, 2003 10:00 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Hi,

I have already find how to use hibernate debug. This is the result of the execution of my application.

Thanks for your help,
Joao Rangel


12:45:25,330 DEBUG SessionImpl:476 - opened session
12:45:25,350 DEBUG Cascades:274 - unsaved-value strategy NULL
12:45:25,350 DEBUG SessionImpl:1278 - saveOrUpdate() unsaved instance
12:45:25,360 DEBUG IncrementGenerator:62 - fetching initial value: select max(chvp) from DocsElement
12:45:25,360 DEBUG IncrementGenerator:76 - first free id: 1
12:45:25,360 DEBUG SessionImpl:732 - saving [FolderEscala#1]
12:45:25,380 DEBUG Cascades:356 - processing cascades for: FolderEscala
12:45:25,380 DEBUG Cascades:364 - done processing cascades for: FolderEscala
12:45:25,390 DEBUG Cascades:356 - processing cascades for: FolderEscala
12:45:25,400 DEBUG Cascades:364 - done processing cascades for: FolderEscala
12:45:25,590 DEBUG SessionImpl:2217 - flushing session
12:45:25,590 DEBUG Cascades:356 - processing cascades for: FolderEscala
12:45:25,590 DEBUG Cascades:364 - done processing cascades for: FolderEscala
12:45:25,590 DEBUG SessionImpl:2326 - Flushing entities and processing referenced collections
12:45:25,600 DEBUG SessionImpl:2645 - Processing unreferenced collections
12:45:25,620 DEBUG SessionImpl:2656 - Scheduling collection removes/(re)creates/updates
12:45:25,620 DEBUG SessionImpl:2229 - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
12:45:25,620 DEBUG SessionImpl:2234 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
12:45:25,620 DEBUG SessionImpl:2264 - executing flush
12:45:25,620 DEBUG NormalizedEntityPersister:499 - Inserting entity: [FolderEscala#1]
12:45:25,650 DEBUG BatcherImpl:192 - about to open: 0 open PreparedStatements, 0 open ResultSets
12:45:25,660 DEBUG BatcherImpl:226 - prepared statement get: insert into DocsElement (name, creation, lastUpdate, chvp) values (?, ?, ?, ?)
12:45:25,660 DEBUG BatcherImpl:232 - preparing statement
12:45:25,660 DEBUG BatcherImpl:192 - about to open: 1 open PreparedStatements, 0 open ResultSets
12:45:25,680 DEBUG BatcherImpl:226 - prepared statement get: insert into DocsFolder (subChvp) values (?)
12:45:25,680 DEBUG BatcherImpl:232 - preparing statement
12:45:25,680 DEBUG BatcherImpl:192 - about to open: 2 open PreparedStatements, 0 open ResultSets
12:45:25,680 DEBUG BatcherImpl:226 - prepared statement get: insert into FolderEscala (subChvp) values (?)
12:45:25,710 DEBUG BatcherImpl:232 - preparing statement
12:45:25,710 DEBUG NormalizedEntityPersister:368 - Dehydrating entity: [FolderEscala#1]
12:45:25,730 DEBUG JDBCExceptionReporter:36 - SQL Exception
java.sql.SQLException: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate insert then in the right order.

Can you show your SQL creation script ?

I know nothing on this subject but try hibernate.jdbc.batch_size=0 on your hibernate.cfg.xml.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: SQL creation script
PostPosted: Tue Oct 28, 2003 2:36 pm 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Sorry, I didn't understand what you mean with "SQL creation script".
Was it these lines?


insert into DocsElement (name, owner_name, creation, lastUpdate, chvp) values (?, ?, ?, ?, ?)

insert into DocsFolder (subChvp) values (?)

insert into FolderEscala (subChvp) values (?)

I tried to set hibernate.jdbc.batch_size=0 and hibernate.jdbc.batch_size=10, and neither of these two values worked.

Thanks for keep trying to help,
Joao Rangel


Top
 Profile  
 
 Post subject: Hibernate bug
PostPosted: Wed Oct 29, 2003 5:53 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Do you think this could be a possible hibernate bug?


Top
 Profile  
 
 Post subject: Re: SQL creation script
PostPosted: Wed Oct 29, 2003 6:45 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
jpgr wrote:
Sorry, I didn't understand what you mean with "SQL creation script".
Was it these lines?


insert into DocsElement (name, owner_name, creation, lastUpdate, chvp) values (?, ?, ?, ?, ?)

insert into DocsFolder (subChvp) values (?)

insert into FolderEscala (subChvp) values (?)

I tried to set hibernate.jdbc.batch_size=0 and hibernate.jdbc.batch_size=10, and neither of these two values worked.

Thanks for keep trying to help,
Joao Rangel


Try to execute SQL from script, mySQL has a very good command line tool for it.


Top
 Profile  
 
 Post subject: Re: SQL creation script
PostPosted: Wed Oct 29, 2003 8:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jpgr wrote:
Sorry, I didn't understand what you mean with "SQL creation script".

What I want it the SQL queries you did to create your DB tables.

CREATE TABLE etc...

I want to know the relationships between your 3 tables (the constraints associated).

This script can be generated by most DB tools.

Quote:
Do you think this could be a possible hibernate bug?

Don't tell that to loud, Gavin may listen ;-) Might be, but I would be surprised.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: You mean the DDLs :)
PostPosted: Wed Oct 29, 2003 10:35 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Hi,

I finally understand that you were refering to the DDLs. Here is the SQL code.

Thanks one more time,
Joao Rangel


create table DocsElement (chvp INTEGER not null, name VARCHAR(255) not null, owner_name VARCHAR(255), creation BIGINT not null, lastUpdate BIGINT not null, primary key (chvp))

create table DocsFolder (subChvp INTEGER not null, primary key (subChvp))

create table FolderEscala (subChvp INTEGER not null, content INTEGER not null unique, primary key (subChvp))

alter table XeDocsFolder add index (subChvp), add constraint FKFDBC8D769098A83F foreign key (subChvp) references DocsElement (chvp)

alter table FolderEscala add index (subChvp), add constraint FKAA12522F9098A83F foreign key (subChvp) references DocsFolder (subChvp)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 10:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, looks like Hibernate does exactly the right thing to me :-)

I think you need to look elswhere for your bug. Perhaps there is an insert trigger on the table, or something.

But clearly, Hibernate performs the inserts in the correct order.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Works for me on Oracle with your mapping file, DDL generated from it, java classes generated from it except for using Long mapping for chvp.

In your DDL, there is "alter table XeDocsFolder". What is XeDocsFolder ?
Did you set proper values to tables (FolderEscala.content which must be unique and not null, ...) ?


To Hibernate Team, the increment generator fails if I don't map my id property to a Long. In 2.0.3, it tries to do object.setId(java.lang.Long).

Does increment generator need a setXX(java.lang.Long) ?

_________________
Emmanuel


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

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.