-->
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.  [ 6 posts ] 
Author Message
 Post subject: update identifier
PostPosted: Thu Dec 22, 2005 2:12 pm 
Newbie

Joined: Tue Dec 20, 2005 2:43 pm
Posts: 4
Hi!

I've got a problem.

Everytime I want to change the Identifier of an object connected to a session
and want to save these changes I get the following Exception:


org.hibernate.HibernateException: identifier of an instance of 'MyObject.Class' altered from 1 to 800

But altering the Identifier from 1 to 800 is exactly what I want to do. Can anybody help me?

Code:


Code:
List myObjects = s.createCriteria(MyObject.class).list();
// this line returns MyObject with Id 1
MyObject mo = (MyObject) myObjects.get(0);
mo.setMyObjectId(800);
s.update(mo);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The very definition of a primary key is that it does not change.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 2:57 pm 
Newbie

Joined: Tue Dec 20, 2005 2:43 pm
Posts: 4
I do not agree with you. Why shouldn't a PK change.

But maybe Hibernate doesn't support this change.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 3:52 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://en.wikipedia.org/wiki/Primary_key


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 8:02 pm 
Newbie

Joined: Fri Dec 23, 2005 1:18 pm
Posts: 7
Hi. If you want to change the PK value in an object, you need to create a new object, copy all values from the first to the second, do the begin transaction, delete the first object, insert the second and commit the transaction.

I think is the unique solution, because Hibernate doesn't allow to change the PK of an object, because this value is used to differentiate it from others objects in the session!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 11:46 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
I like to think of primary keys only relating to the database. In your code, who cares about a primary key (except for the occasional lookup)? I suggest you have another colum that is "unique". A unique column that identifies your object that you can understand just by reading it. It will essetially act the same way and you can change it all you want (and do lookups on them). Almost all of my pojos have a name value. That is mapped to a name column in the table that has a unique constraint on it. Hope this helps.


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