-->
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.  [ 4 posts ] 
Author Message
 Post subject: Composite Keys (Two columns, one of which is a FK)
PostPosted: Thu Jan 04, 2007 5:22 pm 
Newbie

Joined: Wed Oct 26, 2005 1:31 am
Posts: 10
Hibernate version:

3.2.1

I have two tables:

PARENT
--------------------------
PARENT_ID (PK)
.
. other fields
.

CHILD
-------------------------
PARENT_ID (FK)(PK)
INDEX (PK)

So child has a composite ID, a FK to its parent and and an integer for ordering and uniquely identifying the children.

Mapping documents:

Now the mapping for those two tables:
Code:
<class name="Parent" table="PARENT" lazy="true">
  <id column="PARENT_ID" name="parentId" type="long">
    <generator class="native" />
  </id>
  .
  . Other properties....
  .
  <set name="children" inverse="true" cascade="merge,delete-orphan">
   <key column="PARENT_ID" />
   <one-to-many class="Child"/>
  </set>
</class>


<class name="Child" table="CHILD" lazy="true">
<composite-id>      
  <key-many-to-one name="parent">
   <column name="PARENT_ID" not-null="true" />
  </key-many-to-one>
  <key-property name="index" type="long">
    <column name="INDEX" />
  </key-property>
</composite-id>
<many-to-one name="contact" cascade="merge" insert="false" update="false">
  <column name="PARENT_ID" not-null="true" />
</many-to-one>
<property name="index" type="long" column="INDEX" insert="false" update="false"/>
</class>


The Problem

We have a object graph of a parent + children.

We are using merge (on the parent, which cascades to the children) to associate the objects in the graph with the session.

The trouble is this: If the parent and children are both transient (new), then the parent's ID
has not yet been generated. When we attempt to merge the parent and its children into the
session, Hibernate generates a null pointer exception when merging the children because their
parent's ID is null and hasn't yet been generated by hibernate. We can work around this by
using explicitly assigning the parent's ID but we expect that this case should work with hibernate
generating the ID for the parent.


We have opened a bug with an example of the problem

http://opensource.atlassian.com/project ... e/HHH-2326

Anybody have a legitimate workaround that does not require the explicit assignment of the parent ID before merge?

Thanks.

Tyler Van Gorder
tkv@landacorp.com
Landacorp.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 1:07 am 
Newbie

Joined: Fri Jan 05, 2007 12:38 am
Posts: 2
Location: Johannesburg
How about using save() instead of merge() when you know the object is transient.
If you're not sure whether it's transient use saveOrUpdate() instead.

Then cascade should include "save,update"
This works consistently for me.


Top
 Profile  
 
 Post subject: Composite Keys (Two columns, one of which is a FK)
PostPosted: Fri Jan 05, 2007 4:04 am 
Newbie

Joined: Wed Oct 26, 2005 1:31 am
Posts: 10
Quote:
How about using save() instead of merge() when you know the object is transient.
If you're not sure whether it's transient use saveOrUpdate() instead.

Then cascade should include "save,update"
This works consistently for me.


Thanks for the response.

The problem we have with your approach is that our objects have been detached from the session and may or may not have been modified. We do not want to update the record or child record if nothing has been changed. It is my understanding that SaveOrUpdate will update the record (increment the version) every time it is called. Of course, I am new to hibernate, so I may be off base.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 5:33 am 
Newbie

Joined: Fri Jan 05, 2007 12:38 am
Posts: 2
Location: Johannesburg
My understanding of versioning is that the version is only changed if the object is dirty during the flush operation. I have not tested this myself so you could be correct, but I'm sure it's woth a try.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.