-->
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: Changing an assigned identifier
PostPosted: Thu Aug 11, 2005 4:34 am 
Beginner
Beginner

Joined: Sat Feb 26, 2005 12:09 pm
Posts: 27
Location: Helsinki, Finland
What's right way to change an assigned identifier (with cascade) in Hibernate 3.0?

I tried using the setter and calling session.update(obj) immediately after and flushing. I got an exception like this:
org.hibernate.HibernateException: identifier of an instance of com.example.Foo altered from bar to baz

I also tried update with the old id as the explicit second argument, but that did not help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 6:44 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
What do you want to do ?

An id aimed to be immutable during the lifecycle of your domain object. I'am not sure this is possible, for me changing an id means create a new object and delete the old one.

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 10:29 am 
Beginner
Beginner

Joined: Sat Feb 26, 2005 12:09 pm
Posts: 27
Location: Helsinki, Finland
I have DB rows / persistent objects that represent users and groups. The primary key is the login name or group name. There are foreign key references to these tables from elsewhere. When either the login name of a user or a group name changes, it is necessary to update the same DB row and have the change cascade to the references. Creating a new row would break the references.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 10:33 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
hsivonen wrote:
I have DB rows / persistent objects that represent users and groups. The primary key is the login name or group name. There are foreign key references to these tables from elsewhere. When either the login name of a user or a group name changes, it is necessary to update the same DB row and have the change cascade to the references. Creating a new row would break the references.


Changing primary keys is a bad thing to do and should not be done often. It should be far easier to use a little bit of SQL in a separate system (i.e. psql) to make these hopefully infrequent changes.

If these changes are frequent then it may be necessary to rethink your design (if possible).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 10:36 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Use a surrogate key as a primary key instead of your functional key. Then you can change you login/password easily.
This is a best practice advise by hibernate team

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 11:40 am 
Beginner
Beginner

Joined: Sat Feb 26, 2005 12:09 pm
Posts: 27
Location: Helsinki, Finland
scesbron wrote:
Use a surrogate key as a primary key instead of your functional key. Then you can change you login/password easily.
This is a best practice advise by hibernate team


That is the solution I had in place initially. However, my boss does not approve of surrogate keys and wants the SQL schema to be according to his taste, so I need to change the primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 11:44 am 
Beginner
Beginner

Joined: Sat Feb 26, 2005 12:09 pm
Posts: 27
Location: Helsinki, Finland
jamie_dainton wrote:
Changing primary keys is a bad thing to do and should not be done often. It should be far easier to use a little bit of SQL in a separate system (i.e. psql) to make these hopefully infrequent changes.


Is it safe to do this using session.createSQLQuery()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 3:21 am 
Beginner
Beginner

Joined: Sat Feb 26, 2005 12:09 pm
Posts: 27
Location: Helsinki, Finland
hsivonen wrote:
Is it safe to do this using session.createSQLQuery()?


Apparently updates are not allowed in native SQL queries. When I try this through HQL, I hit constraints in the DB. It appears that cascade='all' is not allowed on the id element. Is there a way to specify the cascading of id changes in the Hibernate mapping or do I need to change this manually in the DB?


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.