471,319 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

nhibernate - id change

Hello

i got this simple problem using nhibernate.
I want to chang the id( primary key of the) object
and update it, to change this key.
i me getting nhibernate exception like this:
identifier of an instance of {0} altered from {1} ({2}) to {3} ({4}) // in
place of number, there are values and their types.

can anyone help me?

best regards

roman
Dec 2 '05 #1
8 8247
Have you tried the SourceForge.net discussion forum for NHibernate? That's
really the best place to start for a product-specific question.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Roman" wrote:
Hello

i got this simple problem using nhibernate.
I want to chang the id( primary key of the) object
and update it, to change this key.
i me getting nhibernate exception like this:
identifier of an instance of {0} altered from {1} ({2}) to {3} ({4}) // in
place of number, there are values and their types.

can anyone help me?

best regards

roman

Dec 2 '05 #2
Roman wrote:
i got this simple problem using nhibernate.
I want to chang the id( primary key of the) object
and update it, to change this key.
i me getting nhibernate exception like this:
identifier of an instance of {0} altered from {1} ({2}) to {3} ({4}) // in
place of number, there are values and their types.

can anyone help me?


Yes - you shouldn't be doing that. The point of an identity is that
it's a way of *identifying* something - identities shouldn't change. If
you need to be able to update a piece of data, that data shouldn't be
part of the identity.

My guess is that you're using a natural key (one with business meaning)
as the primary key. From the Hibernate in Action book:

<quote>
Experience has shown that natural keys almost always cause problems in
the long run. A good primary key must be unique, constant, and required
(never null or unknown).
</quote>

Note the "constant" part.

Jon

Dec 2 '05 #3
> Yes - you shouldn't be doing that. The point of an identity is that
it's a way of *identifying* something - identities shouldn't change. If
you need to be able to update a piece of data, that data shouldn't be
part of the identity.

My guess is that you're using a natural key (one with business meaning)
as the primary key. From the Hibernate in Action book:

<quote>
Experience has shown that natural keys almost always cause problems in
the long run. A good primary key must be unique, constant, and required
(never null or unknown).
</quote>


thanks for answers

i know that it should stay intact, but i have to make change possible,
and i want to make it nice way, without deleting object with old id,
and inserting obj with my new id.
i just wanted to have functionality like this:

update TABLE
set ID = 'new_id' where ID = 'old_id'

regards
roman
Dec 2 '05 #4
Roman wrote:

<snip>
thanks for answers

i know that it should stay intact, but i have to make change possible,
and i want to make it nice way, without deleting object with old id,
and inserting obj with my new id.
i just wanted to have functionality like this:

update TABLE
set ID = 'new_id' where ID = 'old_id'


You just can't do that - Hibernate would get very confused about which
object you were talking about.

It sounds like you really need a different primary key, I'm afraid.

(You could go behind Hibernate's back and just make the change directly
to the database - it's not very nice though...)

Jon

Dec 2 '05 #5
>> update TABLE
set ID = 'new_id' where ID = 'old_id'


You just can't do that - Hibernate would get very confused about which
object you were talking about.

It sounds like you really need a different primary key, I'm afraid.

(You could go behind Hibernate's back and just make the change directly
to the database - it's not very nice though...)


i know.
but im affraid that im in the situation in which doing little behind
hibernate
and leave keys as their are right now will be less evil (less work).
after setting new key id in the database object but before saving to
database
i could execute query that will change just the primary key, and later
save whole db object. but it would not be nice :/

regards
roman

Dec 2 '05 #6
i know.
but im affraid that im in the situation in which doing little behind
hibernate
and leave keys as their are right now will be less evil (less work).
after setting new key id in the database object but before saving to
database
i could execute query that will change just the primary key, and later
save whole db object. but it would not be nice :/

regards
roman


i guess i cant do that also, hibernate persister object holds associations
id - databaseobject, and before update its checked if db.key =
parsister.get..key
so i guess i cant do this on open hibernate session.
maybe someone could give me a little bit of advice ?
roman
Dec 2 '05 #7
Roman wrote:
Hello

i got this simple problem using nhibernate.
I want to chang the id( primary key of the) object
and update it, to change this key.
i me getting nhibernate exception like this:
identifier of an instance of {0} altered from {1} ({2}) to {3} ({4})
// in place of number, there are values and their types.


nhibernate can't update a PK of a live object?
Can't you start an update directly on an entity, and provide the
filter yourself?

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Dec 3 '05 #8
Jon Skeet [C# MVP] wrote:
Roman wrote:
i got this simple problem using nhibernate.
I want to chang the id( primary key of the) object
and update it, to change this key.
i me getting nhibernate exception like this:
identifier of an instance of {0} altered from {1} ({2}) to {3}
({4}) // in place of number, there are values and their types.

can anyone help me?


Yes - you shouldn't be doing that. The point of an identity is that
it's a way of identifying something - identities shouldn't change. If
you need to be able to update a piece of data, that data shouldn't be
part of the identity.

My guess is that you're using a natural key (one with business
meaning) as the primary key. From the Hibernate in Action book:

<quote>
Experience has shown that natural keys almost always cause problems in
the long run. A good primary key must be unique, constant, and
required (never null or unknown).
</quote>

Note the "constant" part.


With natural keys you have the problem that if the user makes a typo,
and wants to correct it, the user can't because the object is already
saved. Which is unfortunate and silly, from the user's POV, because the
field is just part of the entity s/he's editing.

Updating PK's isn't that hard for an O/R mapper, after all the
original PK value is known, so the O/R mapper can do: if the entity
isn't new and the pk field is changed (e.g.: different from its
original value), generate an update query where you use the original
value as the PK filter, setting the pk to the new value.

The problem with this is that it has to be a single-entity operation
and the developer has to make sure fk's are updated first (or use
cascading updates inside the db, as it's never known which objects are
related unless all objects are loaded from the db), which can be a
tedious operation. THe developer has to do that manually, because the
O/R mapper can't assume it's to be done automatically, simply because
if the FK changes, the row changes and that can have severe
consequences for the overall system, which therefore shouldn't be
automatic, hence the manual operation.

But besides that, it's a few lines of code inside an O/R mapper core.

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Dec 3 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Aquila Deus | last post: by
1 post views Thread by erin.sebastian | last post: by
5 posts views Thread by gaddoz | last post: by
reply views Thread by beantaxi | last post: by
4 posts views Thread by IceMan | last post: by
3 posts views Thread by Diego Jancic | last post: by
14 posts views Thread by thj | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.