-->
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: Hibernate Mysql Auto Increment key part of a composite key
PostPosted: Tue Jul 17, 2007 3:33 am 
Newbie

Joined: Tue Jul 17, 2007 3:30 am
Posts: 1
Hi

I am facing a problem in saving data to a parent child relationship tables in MySQL. The scenario is

Parent Table with Auto Increment Key (Database Generated AUTO_INCREMENT of MySQL).

Child Table with Composite Key (One part of the same is the parent tables key and other is a user defined value).

The Mapping for Parent Table contains the ID declaration and the association to child as
<id name="parentPk" type="int">
<column name="PARENT_PK" />
<generator class="native" />
</id>
<set name="child" inverse="true" cascade="all">
<key>
<column name="parent_id" not-null="true" />
</key>
<one-to-many class="childClass" />
</set>

For the Child Table Two Mappings have been tried
Mapping 1)
<composite-id name="id" class="CompositeId.class">
<key-property name="child.user.define.key" type="string">
<column name="childPk1" length="50" />
</key-property>

<key-property name="parentPk" type="int">
<column name="parent_id" not-null="true" />
</key-property>
</composite-id>

<many-to-one name="parent" class="parent.class" insert="false" update="false">
<column name="parent_id" not-null="true" />
</many-to-one>
With the above mapping, a database integrity constrain violation error is received. Hibernate does not set the foreign key into the child table by retrieving the primary key from the parent table. (Tried the identity generator as well).

Mapping 2)
<composite-id name="id" class="CompositeId.class">
<key-property name="child.user.define.key" type="string">
<column name="childPk1" length="50" />
</key-property>

<key-many-to-one name="parentPk" class="parent.class">
<column name="parent_id" not-null="true" />
</key-property>
</composite-id>

<many-to-one name="parent" class="parent.class" insert="false" update="false">
<column name="parent_id" not-null="true" />
</many-to-one>
With this mapping Hibernate throws an IllegalArgumentException from BasicGetter class. On further investigation, it is evident that Hibernate is trying to invoke get method on java.lang.Integer rather than on the parent class.

Any help on this problem would be appreciated.

Regards
Tarun


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.