-->
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.  [ 5 posts ] 
Author Message
 Post subject: about composite key
PostPosted: Tue Jan 06, 2004 2:07 am 
Beginner
Beginner

Joined: Thu Dec 04, 2003 3:47 am
Posts: 31
Location: Hong Kong
Say I have class C, which is a children class of class P, and the class P contains a composite key, persisted as class compID, where the identifier of compID is a identity key.

I have a problem

the foreign key of C references P is also composite. I have to first instantiate compID, and assign it to P. After that, I reference P from C.

If I persist C first, one of the the foreign key in C reference to P, which is P's identity key, would not be correct since it is not generated yet before P is persisted.

If I persist P first, the value of the foreign key in C reference to P, which is P's identity key, would also not be correct.

What is the correct way of doing so? I read the document, from 6.4 As composite Identifiers,

"Since a composite identifier must be assigned to the object before saving it, we can't use unsaved-value to distinguish between newly instantiated instancesand instances saved in a previous session. You should instead implmenet Interceptor.isUnsaved() if you wish to use saveOrUpdate() or cascading save/update."

Where can I find more detail explanation of the above paragraph? or sample code illustrating it? I'm not sure whether it is the cause of the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 9:33 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Hi, I've been working with compound keys a bit lately. Here's what may help you.

If you can change the database, then do so, as it will make it all so much easier.
If you can't,
Check out the Classes Inner, Middle and Outer, in the test.src directory of the distribution.

Make sure your key classes implement a good hashcode/equals.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 9:36 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
One more thing- as far as the unsaved-value problem. Hibernate can't detect a new vs. existing entity in the database when you use a composite key. In 2.1 you can get around this by telling hibernate to look at a Timestamp field. It's in the docs. Something like this:

<timestamp column="LAST_UPDATED" unsaved-value="null" >

This allows hibernate to know whther to fire an insert vs update, since it can't tell based on the key.

James


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 11:21 am 
Beginner
Beginner

Joined: Thu Dec 04, 2003 3:47 am
Posts: 31
Location: Hong Kong
Thx jlawmi! I will try your suggestion!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jlawmi wrote:
<timestamp column="LAST_UPDATED" unsaved-value="null" >

This allows hibernate to know whther to fire an insert vs update, since it can't tell based on the key.

Correct, I prefer using <version> which is far more safe than <timestamp>

_________________
Emmanuel


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