aspose file tools*
The moose likes Object Relational Mapping and the fly likes Cannot add or update a child row: a foreign key constraint fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Cannot add or update a child row: a foreign key constraint fails" Watch "Cannot add or update a child row: a foreign key constraint fails" New topic
Author

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

Baktha Elumalai
Greenhorn

Joined: Mar 13, 2010
Posts: 28
I have two tables
xydataa and fm4featuress

this is my application

Xytaaa.hbm.xml

<?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">
<!-- Generated Mar 19, 2010 4:59:08 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="navl" name="test.Xydataa" table="xydataa">
<id column="ID" name="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="longi" type="string">
<column name="Longi"/>
</property>
<property name="lat" type="string">
<column name="Lat"/>
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true"/>
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true"/>
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true"/>
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true"/>
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true"/>
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true"/>
</property>
<property name="gpsTime" type="string">
<column length="19" name="GpsTime" not-null="true"/>
</property>
<property name="location" type="string">
<column name="Location"/>
</property>
<property name="city" type="string">
<column name="City"/>
</property>
<property name="state" type="string">
<column name="State"/>
</property>
<property name="distance" type="string">
<column length="100" name="Distance"/>
</property>
<property name="times" type="timestamp">
<column length="19" name="Times"/>
</property>
<many-to-one name="fm4featuress" class="test.Fm4Featuress" cascade="all" insert="false" update="false">
<column name="ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>


Fm4featuress.hbm.xml

<?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">
<!-- Generated Mar 19, 2010 4:59:08 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="navl" name="test.Fm4Featuress" table="fm4featuress">
<id column="ID" name="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="DataId" type="int">
<column name="DataId" not-null="true"/>
</property>
<property name="Value" type="int">
<column name="Value" not-null="true"/>
</property>
<set name="xydataa" inverse="true" table="xydataa" lazy="true">
<key>
<column name="ID" not-null="true"/>
</key>
<one-to-many class="test.Xydataa" />
</set>

</class>
</hibernate-mapping>


it's the main file





i have to use many to one in Xydataa and one to many in Fm4featuress

i run the application i got the following error

StackTrace

SEVERE: Cannot add or update a child row: a foreign key constraint fails (`navl`.`fm4featuress`, CONSTRAINT `lkj` FOREIGN KEY (`ID`) REFERENCES `xydataa` (`ID`) ON DELETE CASCADE)
SEVERE: null
org.hibernate.exception.ConstraintViolationException: could not insert: [test.Fm4Featuress]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at tcplistener.Alldatabase.Database(Alldatabase.java:172)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`navl`.`fm4featuress`, CONSTRAINT `lkj` FOREIGN KEY (`ID`) REFERENCES `xydataa` (`ID`) ON DELETE CASCADE)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 18 more


what is the wrong in my code


Regards
Bakthavachalam E
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot add or update a child row: a foreign key constraint fails