CompositeId Syntax?

133 views
Skip to first unread message

Tonya

unread,
Jan 20, 2010, 2:28:13 PM1/20/10
to Fluent NHibernate
Hi,

I have a table with a composite key that consists of Id and Version
columns. What is the syntax for using the CompositeId() method in a
mapping?

Thanks,
Tonya

Hudson Akridge

unread,
Jan 20, 2010, 2:54:38 PM1/20/10
to fluent-n...@googlegroups.com
CompositeId().KeyProperty(x => x.Id).KeyProperty(x => x.Version);

Protip: Avoid composite Id's. Guessing by your version column, I'm assuming that you want this Id in place so when you change your entity (update the version in the business domain, say on a policy that has been changed or something) you want any of the associated entities to be wiped (by nature of the compositeId to the parent no longer being valid) and you want to put in new values for your entity's associations (new credit cards for a policy, as was discussed in another thread recently).

If you have control over the database, and this isn't a legacy db, then there are other, more DDD/OO methods of performing this. A contract with an entity NHibernate has for the Id states at the simplest level, that the Id to an entity should never change, otherwise it's a different entity. What you're going to now have to keep track of, is letting NHibernate know how to manage the entityId changing every time a newer version of it is created, and if it's the same object, you're going to have to flush your session and 2nd level cache all the time, rendering them fairly useless.

Just food for thought :)

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.






--
- Hudson
http://www.bestguesstheory.com
http://twitter.com/HudsonAkridge

Tonya

unread,
Jan 20, 2010, 3:04:40 PM1/20/10
to Fluent NHibernate
Thank you...for the syntax and the advice. Hopefully I can rework the
schema to avoid to composite keys.
Reply all
Reply to author
Forward
0 new messages