wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Getting an error when trying to insert data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Getting an error when trying to insert data" Watch "Getting an error when trying to insert data" New topic

Getting an error when trying to insert data

Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
Hi there,

I have 2 tables and one of them has a foreign key on the other table. I insert the data in to the first table which gets inserted properly. When I try to insert data in the child table which has a foreign key on the first table, I get an error saying parent key not found... here is the dump. I Cannot understand as to why this happen when the data which is a foreign key is already inserted...

Caused by: com.nasd.fms.framework.database.DaoException: Exception in HibernateDao.insert()!
at com.nasd.fms.framework.database.HibernateDao.insert(HibernateDao.java:250)
at com.nasd.fms.framework.database.HibernateDao.insert(HibernateDao.java:220)
at com.nasd.mrdt.MRDTMappingDriver.prepareDataForSavings(MRDTMappingDriver.java:90)
at com.nasd.fms.xread.MappingManagerImpl.Transform(MappingManagerImpl.java:145)
... 20 more
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.flush(Unknown Source)
at com.nasd.fms.framework.database.HibernateDao.insert(HibernateDao.java:239)
... 23 more
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (NMAOWNER.SYS_C002743) violated - parent key not found

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:458)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4105)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
Paras Jain
Ranch Hand

Joined: Feb 26, 2005
Posts: 137
Looks like there is some problem in the hibernate mapping file. Please post the contents of the hbm mapping files for parent and child object

Paras Jain
SCJP 5.0
Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
I actually got it... The relationship had to defined using a different approach

I had to use this

<!-- bi-directional many-to-one association to ApplicantFirm -->
<!-- bi-directional many-to-one association to ApplicantFirm -->
class="com.nasd.fms.framework.database.hibernate.pojo.ApplicantFirm" insert="false" update="false">
<column name="FIRM_CRD_NB" />
<column name="FLNG_SEQ_NB" />

instead ofusing key-many-to-one...
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
Have a look at these use cases
It is sorta covered in the JavaRanch Style Guide.
subject: Getting an error when trying to insert data