In an attempt to provide full disclosure I am a complete NOOBIE when it comes to NHibernate. In fact I have only been using it for a few weeks or so. What this means is take everything I say in this post with a grain of salt cause there may be a better, easier way. As I am in the process of learning how to use Nhibernate and have choosing to user FluentNhibernate to setup my mappings. While I am learning this stuff I thought it would be useful to someone (trust me there is that ONE person out there) to do a brain dump of my findings.
Anyway, onto the meat of the post. Today I ran into the need to create a mapping to a table which was keyed via Composite keys.
Below is the code needed to achieve this via FluentNhibernate
WithTable( "C_Diagnosis" );
.WithKeyProperty( x => x.DiagnosisID, "DIAGNOSIS_ID" )
.WithKeyProperty( x => x.DiagnosisSetID, "DIAGNOSIS_SET_ID" );
Map( x => x.Code, "CODE" );
Below is the output .xml file which was generated (for all you out there NOT use FluentNhibernate)
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="FULLNAMESPACEHERE.Common" namespace="FULLNAMESPACEHERE.Common.Entities">
<class name="ClinicalDiagnosisInformation" table="C_Diagnosis" xmlns="urn:nhibernate-mapping-2.2">
<key-property type="Int32" name="DiagnosisID" column="DIAGNOSIS_ID" />
<key-property type="Int32" name="DiagnosisSetID" column="DIAGNOSIS_SET_ID" />
<property name="Code" column="CODE" length="100" type="String">
<column name="CODE" />
As you can see setting up a composite key is pretty easy and pretty straight forward.
Till next time,
01-16-2009 4:10 PM