-->
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: Insertion problem in 2 parent table, 1 child table scenario
PostPosted: Wed Sep 27, 2006 10:30 am 
Newbie

Joined: Wed Sep 27, 2006 3:34 am
Posts: 7
Hi,
I am not able to insert in 2 parent table, 1 child table scenario.
I have 2 parent tables(say P1, P2), 1 child table(say C).
Primary keys: P1-->p1; P2-->p2; C-->p1+p2(These keys are foreign keys and primary keys).

In hbm files, the mapping given as below:
Parent P1 has Set mapping to child C.
Parent P2 has Set mapping to child C.
Child C has <key-many-to-one mapping to both Parents P1, P2.

I took domain Object for P1 populated with P2 and C.
When I call session.save(p1_domainObject); hibernate generates insert query for P1, C. Since P2 does not have record, getting exception: ConstraintViolationException: violated - parent key not found

I want to insert into both the Parents and child by saying session.save(p1_domainObject);. Please suggest me mapping.

Thanks in advance.

Mapping for Child table
Code:

<hibernate-mapping>
   <class name= "Child_Class"
      table="Child_Table">
      <composite-id>
         <key-many-to-one name="parent1" class="Parent1" column="p1" />
         <key-many-to-one name="parent2" class="Parent2" column="p2" />
      </composite-id>
     </class>
</hibernate-mapping>


Mapping for Parent Table-1
Code:
<hibernate-mapping>
   <class name= "Parent1"
      table="Parent_Table_1" dynamic-insert="false">
      <id name="p1" column="p1" type="java.lang.String">
         <generator class="assigned"/>
      </id>
      <set name="userKeyValue" table="Child_Table" lazy="true" inverse="true" cascade="all">
         <key column="p1"/>
         <one-to-many class="Child_Class"/>
      </set>
     </class>
</hibernate-mapping>


Mapping for Parent Table-2
Code:
<hibernate-mapping>
<class name= "Parent2"
   table="Parent_Table_2" dynamic-insert="false">
   <id name="p2" column="p2" type="java.lang.String">
      <generator class="assigned"/>
   </id>
       <set name="userKeyValue" table="Child_Table" lazy="true" inverse="true" cascade="all">
      <key column="p2"/>
      <one-to-many class="Child_Class"/>
   </set>
</class>
</hibernate-mapping>


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.