-->
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: integrity constraint violated - parent key not found
PostPosted: Wed Aug 12, 2015 8:15 am 
Newbie

Joined: Wed Aug 12, 2015 7:56 am
Posts: 2
I have a one-to-many relationship between Employee and Certificate

employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Employee" table="EMPLOYEE" >

<meta attribute="class-description">
This class contains the employee details.
</meta>

<id name="id" type="int" column="ID">
<generator class="sequence">
<param name="sequence">EMP_SEQ</param>
</generator>
</id>
<set name="certificates" cascade="all" table="CERTIFICATE" inverse="true" >
<key column="employee_id"/>
<one-to-many class="hibernate.Certificate" />
</set>

<property name="firstName" type="string" column="FIRST_NAME"/>
<property name="lastName" type="string" column="LAST_NAME"/>
<property name="salary" type="int" column="salary"/>

</class>
</hibernate-mapping>

Certificate.hbm.xml

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Certificate" table="CERTIFICATE">
<meta attribute="class-description"> This class contains the certificate records. </meta>
<id name="id" type="int" column="ID">
<generator class="sequence">
<param name="sequence">cert_id</param>
</generator>
</id>

<property name="name" column="certificate_name" type="string"/>
<property name="empid" column="employee_id" type="int"/>
</class>
</hibernate-mapping>

hibernate.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:XE
</property>
<property name="hibernate.connection.username">aparna</property>
<property name="hibernate.connection.password">aparna1local</property>
<property name="hibernate.show_sql">true</property>
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hbm2ddl.auto">create</property>
<!-- <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> -->

<!-- List of XML mapping files -->
<mapping class="hibernate.Employee" resource="resources/Employee.hbm.xml"/>
<mapping class="hibernate.Certificate" resource="resources/Certificate.hbm.xml"/>

</session-factory>
</hibernate-configuration>


Though Sequence is generated correctly, insert Query for Certificate is picking up foreign_key as 0 and hence exception is thrown

17:39:23.609 [main] DEBUG org.hibernate.SQL - select EMP_SEQ.nextval from dual
Hibernate: select EMP_SEQ.nextval from dual
17:39:23.698 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Integer[1]]
17:39:23.699 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 1, using strategy: org.hibernate.id.SequenceGenerator
17:39:23.781 [main] DEBUG org.hibernate.SQL - select cert_id.nextval from dual
Hibernate: select cert_id.nextval from dual
17:39:23.785 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Integer[1]]
17:39:23.785 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 1, using strategy: org.hibernate.id.SequenceGenerator
17:39:23.786 [main] DEBUG org.hibernate.SQL - select cert_id.nextval from dual
Hibernate: select cert_id.nextval from dual
17:39:23.787 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Integer[2]]
17:39:23.787 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 2, using strategy: org.hibernate.id.SequenceGenerator
17:39:23.788 [main] DEBUG org.hibernate.SQL - select cert_id.nextval from dual
Hibernate: select cert_id.nextval from dual
17:39:23.788 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Integer[3]]
17:39:23.789 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 3, using strategy: org.hibernate.id.SequenceGenerator
17:39:23.789 [main] DEBUG o.h.e.t.spi.AbstractTransactionImpl - committing
17:39:23.790 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
17:39:23.792 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
17:39:23.797 [main] DEBUG o.h.engine.internal.Collections - Collection found: [hibernate.Employee.certificates#1], was: [<unreferenced>] (initialized)
17:39:23.813 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 4 insertions, 0 updates, 0 deletions to 4 objects
17:39:23.814 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
17:39:23.819 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
17:39:23.820 [main] DEBUG o.h.internal.util.EntityPrinter - hibernate.Certificate{empid=0, name=PMP, id=3}
17:39:23.820 [main] DEBUG o.h.internal.util.EntityPrinter - hibernate.Employee{firstName=Naik, lastName=AParna, certificates=[hibernate.Certificate#1, hibernate.Certificate#2, hibernate.Certificate#3], id=1, salary=30000}
17:39:23.820 [main] DEBUG o.h.internal.util.EntityPrinter - hibernate.Certificate{empid=0, name=MBA, id=1}
17:39:23.820 [main] DEBUG o.h.internal.util.EntityPrinter - hibernate.Certificate{empid=0, name=MCA, id=2}

17:39:23.871 [main] DEBUG org.hibernate.SQL - insert into EMPLOYEE (FIRST_NAME, LAST_NAME, salary, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE (FIRST_NAME, LAST_NAME, salary, ID) values (?, ?, ?, ?)
17:39:23.904 [main] DEBUG org.hibernate.SQL - insert into CERTIFICATE (certificate_name, employee_id, ID) values (?, ?, ?)
Hibernate: insert into CERTIFICATE (certificate_name, employee_id, ID) values (?, ?, ?)
17:39:23.940 [main] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
java.sql.SQLException: ORA-02291: integrity constraint (APARNA.FK_1T5PCA769LSFF735CNWKXBDQ4) violated - parent key not found

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) ~[ojdbc14.jar:Oracle JDBC Driver version -


Top
 Profile  
 
 Post subject: Re: integrity constraint violated - parent key not found
PostPosted: Wed Aug 12, 2015 12:11 pm 
Newbie

Joined: Wed Aug 12, 2015 7:56 am
Posts: 2
Setting this property in hibernate.cfg.xml created the table definition for Certificate table with Foreign Key as Not NULL and I guess Hibernate tried to put a value of 0 in the Employee ID field. I dropped the table, created it again with Employee ID (Foreign key) as NULL and deleted below entry. Its working fine now.
<property name="hbm2ddl.auto">create</property>


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.