Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Using FluentNhibernate to map composite keys for a table

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

public ClinicalDiagnosisInformationMap()
{
	WithTable( "C_Diagnosis" );

	UseCompositeId()
                .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">
    <composite-id>
      <key-property type="Int32" name="DiagnosisID" column="DIAGNOSIS_ID" />
      <key-property type="Int32" name="DiagnosisSetID" column="DIAGNOSIS_SET_ID" />
    </composite-id>
    <property name="Code" column="CODE" length="100" type="String">
      <column name="CODE" />
    </property>
  </class>
</hibernate-mapping>

As you can see setting up a composite key is pretty easy and pretty straight forward.

Till next time,


Posted 01-16-2009 4:10 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

DotNetShoutout wrote Using FluentNhibernate to map composite keys for a table
on 01-16-2009 8:06 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Arjan`s World » LINKBLOG for January 17, 2009 wrote Arjan`s World &raquo; LINKBLOG for January 17, 2009
on 01-17-2009 4:29 PM

Pingback from  Arjan`s World    &raquo; LINKBLOG for January 17, 2009

Dew Drop – January 17, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop &ndash; January 17, 2009 | Alvin Ashcraft's Morning Dew
on 01-17-2009 9:28 PM

Pingback from  Dew Drop &ndash; January 17, 2009 | Alvin Ashcraft's Morning Dew

Derik Whittaker wrote Many-to-One Mapping with Composite-ID’s with FluentNhibernate
on 01-22-2009 9:28 PM

In an attempt to provide full disclosure I am a complete NOOBIE when it comes to NHibernate.&#160; In

Community Blogs wrote Many-to-One Mapping with Composite-ID’s with FluentNhibernate
on 01-22-2009 9:37 PM

In an attempt to provide full disclosure I am a complete NOOBIE when it comes to NHibernate.&#160; In

Nikola Malovic wrote re: Using FluentNhibernate to map composite keys for a table
on 01-24-2009 5:22 AM

Right on!

Thanks,

Nikola

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)