-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to change primary key?
PostPosted: Sun Apr 16, 2006 8:59 pm 
Newbie

Joined: Sun Apr 16, 2006 8:53 pm
Posts: 2
The assigned primary key need to be modified。 How to do this?
Should I first delete the original record with original key and create a new one with the new key?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 9:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yes


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 11:34 pm 
Newbie

Joined: Sun Apr 16, 2006 8:53 pm
Posts: 2
david wrote:
Yes


Thanks.

But if we use pure sql, can't we use one sql to update the primary key? as following:

Update table1 set id=newid where id=oldid


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 11:52 pm 
Regular
Regular

Joined: Wed Feb 22, 2006 11:28 am
Posts: 65
Location: Santiago, Chile
Hello Friend:

I guess you can do it. But, always with caution of the "repeated key". I will do that update operation with HQL.

requests for comments.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 2:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You can choose to by-pass the ORM and update the key directly. I would suggest that you use the ORM the way it is designed to be used. In this case, the primary key should be treated as imutable, eg, the setId domain method should be private. Thus you should deleted the old object and create a new one as required.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 1:03 pm 
Newbie

Joined: Wed Apr 26, 2006 12:57 pm
Posts: 2
we should remove and create a new record? OK, that's all right. but what if my record had a million references on many other tables?
should i delete them too or re-reference them again?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Well it all depends. Data cleanup operations would normally be completed using database scripts so that all of the Primary Key changes etc can occur efficiently etc. If this is a part of an application use-case then it is not ideal and does not fit a quality DB design. In this sort of case, use a native query or JDBC and update it directly. Care is required when doing this sort of work.
Your the engineer so your in the best position to choose themost appropriate approach for your system and its limitations/constraints.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 12:09 am 
Newbie

Joined: Wed Apr 26, 2006 12:57 pm
Posts: 2
allright, but can you possibly tell why why ORM frameworks lack this simple ability? is there a specific reason?
i mean, we could update a PK in Hibernate and then let the DB re-reference all the referenced data by its "UPDATE CASCADE" feature. can't we?
or while we can manage this re-referencing with scripts or DB, why shouldn't we manage that completely with Hibernate?


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