Using NHibernate's ISession.Get<>() w/ a composite key

David Pfeffer picture David Pfeffer · Feb 16, 2010 · Viewed 7.8k times · Source

I have a composite key in a database table / NHibernate entity. Can I somehow use the .Get method to grab a specific entity or do I have to use HQL / Criteria due to the composite key?

Answer

Christian Specht picture Christian Specht · Jan 28, 2011

With this composite key mapping:

<class name="MyClass">
    <composite-id>
        <key-property name="Key1" />
        <key-property name="Key2" />
    </composite-id>
    <property name="..." />
</class>

...you can use .Get like this:

var x = Session.Get<MyClass>(new MyClass() { Key1 = 'Foo', Key2 = 'Bar'});