Reputation: 65
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.
Upvotes: 1
Views: 360
Reputation: 5958
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!
Upvotes: 0
Reputation: 52725
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.
Upvotes: 1