views:

44

answers:

2

I have a situation where I have tables that have many columns used as a composite primary key,

worse off business logic requires these to be mutable.

I am using nhibernate and have no problems mapping for loadig/saving these. However I need to be able to update the property values and have these changes be reflected in the DB when i call update.

This is a legacy system so there is no changing of the db structure(its horrible).

Is there anything I can do with nhibernate to resolve this problem ? does nhibernate even allow its keys to be modified.

I am using: .net4.0, nhibernate 2.1, fluentnh 1.0, and sql server as the backend.

A: 

You can't change the PK with NHibernate. As you've already figured out, the structure you are dealing with is just horrible.

What you can do is use SQL queries to change the PK. Keep in mind you'll have to reload the entity with the new id before using it again.

Diego Mijelshon
I was hoping there was just something i been missing that I could tell nhibernate how to handle this :(. I thought about using custom sql to handle the changes but then that just defeats the purple of nh. The saddest thing is that almost every table has this setup and no surrogate keys. I had such a nice domain going too...sigh. I'm just going to use linq2sql to get this going. Thanks for the help diego.
Dovix
A: 

Actually you are not obliged to map the database schema as-is to a domain object.

I have an application that has a an object structure that cannot really be represented on the database with NHibernate understanding the semantics: it would require a 3 column composite-id but the catch is that there are cases that 2 of the columns get modified. So i mapped a single column-property as PK and automated the usage of the other two and got the best of two worlds.

Note that in these cases 2nd-level caching is a big no-no!

Jaguar
I try this approach and it seemed to work fine. However since those there is only 1 primary key. Updates to an entity would send updates to all the rows and not just that one row the entity really corresponds to. So this would be generatedUPDATE accountAccess SET access = 400WHERE user = 1instead of UPDATE accountAccess SET access = 400WHERE user_id = 1and accoun_id = 2In the first all user account access items are updated which is wrong. The second is right. Thanks for the help bud.
Dovix