-->
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.  [ 1 post ] 
Author Message
 Post subject: Cannot addorupdate a child row:a foreign key constraintfails
PostPosted: Wed Mar 31, 2010 4:44 am 
Newbie

Joined: Wed Mar 31, 2010 4:35 am
Posts: 19
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>
<!-- <id name="id" unsaved-value="any">
<generator class="assigned" />
</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

Session sess = null;
Transaction tr = null;
sess = HibernateUtil.getSessionFactory().openSession();
try
{
tr = sess.beginTransaction();
Date resultdate = new Date(times);
xx = new DecimalNumber(x,7);
yy = new DecimalNumber(y,7);
System.out.println("Inserting xydataa");
Xydataa xy=new Xydataa();
Fm4Featuress fm = new Fm4Featuress();
Set fm1 = new HashSet();
xy.setLongi(xx+"");
xy.setLat(yy+"");
xy.setAltitude(altitude);
xy.setAngle(angle);
xy.setSatellite(satellites);
xy.setSpeed(speed);
xy.setObjectId(objectId);
xy.setClientId(clientId);
xy.setGpsTime(sdf.format(resultdate));
xy.setLocation(location);
xy.setCity(city);
xy.setState(state);
xy.setDistance(distance);
sess.save(xy);
System.out.println("Done");
tr.commit();


for (int i = 0; i < obj.length && obj[i] != 0; i++)
{
tr = sess.beginTransaction();
int all = obj[i] - 1;
System.out.println("Inserting Fm4Featuress");
fm.getXydataa().add(xy);
fm.setXydataa(fm1);
xy.setfm4featuress(fm);
//fm1.add(xy);
fm.setDataId(all);
fm.setValue(obja[i]);
sess.save(fm);
tr.commit();
}

System.out.println("Commit");
}catch(Exception exx)
{
Logger.getLogger(Alldatabase.class.getName()).log(Level.SEVERE, null, exx);

}finally
{
sess.close();
}


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

i run the application i got the following error

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


i need to get last insert id from the xydataa and put in to the fm4featuress

what is the wrong in my code

pls help me


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

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.