views:

40

answers:

2

Hi,

How do I get NHibernate to save or update using column other than the <id>? I've implemented an identity key column in a table but this is not what makes a row unique. I know that "composite-id" exists but I heard that that should only be used on legacy databases, where there is not much freedom to change it. Is there any other way to implement unique keys without using an artifical key? Should I make my own key based on a hash of the columns that make a row unique? If I do that, won't NHibernate complain when it tries to insert an object with a manually assigned key?

+3  A: 

NHibernate has lots of options for primary keys. You can use any of the available built-in generators, or an application-assigned PK (the "assigned" generator option).

If none of these options suit you, you can always implement your own IIdentifierGenerator, but think really hard before doing so: the built-in generators and PK handling encapsulate years of RDBMS experience from many experts.

Composite PKs are generally not worth the complexity they add.

Mauricio Scheffer
How do I save an object with an assigned key? I keep getting the error "a different object with the same identifier value was already associated with the session"
SideFX
@SideFX: that's a whole other question, barely related to this one, and you have already asked it: http://stackoverflow.com/questions/3470648/saving-manually-created-objects-using-nhibernate
Mauricio Scheffer
Thanks you. I appreciate your help.
SideFX
The <version> tag will also allow me to do this.
SideFX
+1  A: 

Uniqueness of db rows is completely different then identifying which row you want to update.

Why not use unique constraints to ensure your records are unique and follow the standard practice of using Ids.

ShaneC