473,719 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8471
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2857
by: Aquila Deus | last post by:
The documents on their website are completely wrong. Following is the a real example that works with NHibernate 1.0.2.0: <?xml version="1.0" encoding="utf-8"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <configSections> <!-- SecurityException is thrown if NHibernate assembly is not in GAC. --> <section name="hibernate-configuration"
1
13708
by: erin.sebastian | last post by:
Hello All, I am new to hibernate and new to nHibernate. I created a small app and when i try and run it i get the error i posted at the bottom of this message. I am using the helper class that can be found here http://blogs.intesoft.net/simon/articles/16.aspx#36 . Does anyone know why this error might occur.. i have NO IDEA.... if you require anymore information please let me know and i will send it through. Thanks in advance! Erin
5
9568
by: gaddoz | last post by:
Hi all, I'm trying to set up a .NET webservice using in data access layer Nhibernate (nhib 1.2.0 beta). The first trouble I met was that a webservice cannot serialize Interfaces, like IList<>. After I workarounded it, now I get: System.InvalidOperationException: There was an error generating the XML
0
4887
by: beantaxi | last post by:
Hello all, I'm trying to write a simple program, using VS C# 2005 Express and SQL Server Express with NHib 1.0.2. No matter what I try, I'm unable to connect: cannot open connection at NHibernate.Impl.SessionFactoryImpl.OpenConnection() at NHibernate.Impl.SessionImpl.get_Connection() at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel
5
17772
by: ronlahav | last post by:
Dear all i Wonder how can i edit a node property (in C# not by hand :) ) i have the follwing XMl file (hibernate.cfg.xml) <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" > <session-factory name="NHibernate.Test"> <!-- properties -->
0
3465
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement IUserCollectionType to my custom list, which I did. But I still get an error when trying to read the object: " {"Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1' to
4
3333
by: IceMan | last post by:
Hi, I am using the configuration object and passing it a Assembly object to load. The assembly object i pass in is from a dll which is in a different folder then the Nhibernate dll. The AddAssembly(Assembly assembly) function throws me an Error saying it can't load the configuration hbm.xml file because it can't load the assembly in the hbm.xml file. Why does NHibernate try to load the Assembly again??
3
3164
by: Diego Jancic | last post by:
Hi! Hope you can help my with you issue... I made a domain model (http://cedev.com.ar/model.jpg) and what I'm trying to is the NHibernate mappings to persist it. (It's uses the Decorator Pattern, of course..) It's importante to know that a Person might be a Student and a Tutor at the same time (that's why it's not a simple inheritance) I created a table for ConcretePerson, another one for SystemUser (only with the Id), other for Tutor...
14
3686
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
0
8827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8724
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7991
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4481
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.