Click here to Skip to main content
15,881,852 members
Articles / Database Development

Join on tables using Fluent NHibernate

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 Jan 2011CPOL2 min read 67.6K   5   4
Join on tables using Fluent NHibernate

I started using Fluent NHibernate about a year ago as I was experimenting with ORMs in general when a good friend introduced me to the framework.

First of all, I felt that learning Fluent NHibernate in the first place wasn't that easy. I eventually found everything I needed on the internet, but for some reason, it wasn't as easy for me as other frameworks I've dealt with in the past.

Enough said, there were some things that I couldn't find how to do on the internet, like how to join two different tables using Fluent NHibernate mappings. I even spotted some people thirsty for answers...

Check out this question on stackoverflow.com that I found as I was looking for the answer myself, and eventually ended up coming back to answer the question myself! :)

I'll show this using a simple example.

I have two entities:

C#
public class FormStructure
{
    public virtual Int32 FormId { get; private set; }
    public virtual Int32 FormType { get; set; }
    public virtual FormField FieldId { get; set; }
}

public class FormField
{
    public virtual int FieldId { get; private set; }
    public virtual String FieldName { get; set; }
    public virtual int? FieldType { get; set; }
    public virtual int? DisplayOrder { get; set; }
}

Now, I want to be able to create a query on the FormStructure entity, and have the results ordered by the matching DisplayOrder field in the FormField entity. I also want the DisplayOrder field to be available to me as a property of the FormStructure entity.

In order to accomplish this, I will need to create a Join between these two tables. The first step is to add the DisplayOrder field as a property in the FormStructure entity:

C#
public virtual int? DisplayOrder { get; set; }

Then, all I needed to do was use the Join method on my mapping class like this:

C#
public class FormStructureMap : ClassMap<formstructure>
{
    public FormStructureMap()
    {
        Table("FormStructure");

        Id(x => x.Id);
        Map(x => x.FormType);
        References(x => x.Schedule).Column("ScheduleId");
        References(x => x.Field).Column("FieldId");
        Map(x => x.IsMandatory).Nullable();

        Join("FormFields", m =>
        {
            m.Fetch.Join();
            m.KeyColumn("FieldId");
            m.Map(t => t.DisplayOrder).Nullable();
        });
    }
}

Now the query will automatically be created as a join behind the scenes, and I will have the DisplayOrder column available for me in every row.

This might remove some of the rows from the result, if they have NULL values in the added column from the join. To avoid this, add 'm.Optional()' inside the Join method.

In order to query the table with the join like I defined, all I need to do is something like this:

C#
return session.CreateCriteria<formstructure>()
              .Add(Expression.Eq("FieldName", fieldName))
              .AddOrder(Order.Asc("DisplayOrder"))
              .List<formstructure>();

That's all for now.
I think I'll be blogging more about Fluent NHinernate in future posts as well, as I have started using it more heavily lately...


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
GeneralManaging CRUD Operations in Fluent Nhibernate Pin
Paul Brower8-Feb-11 9:13
Paul Brower8-Feb-11 9:13 
Gilly, I'm surprised there aren't more articles on CP about Fluent NHibernate. I thought since you had posted this blog, that I might trouble you with a fluent nhibernate question:

Do any of your winform projects (using fluent nhibernate) allow users to directly edit data in a grid? If so, would you mind sharing the pattern you use to allow user to cancel the changes they have made in the grid?
GeneralRe: Managing CRUD Operations in Fluent Nhibernate Pin
Gilly Barr9-Feb-11 8:32
Gilly Barr9-Feb-11 8:32 
GeneralRe: Managing CRUD Operations in Fluent Nhibernate Pin
Simon_Whitehead24-Mar-12 21:10
Simon_Whitehead24-Mar-12 21:10 
GeneralRe: Managing CRUD Operations in Fluent Nhibernate Pin
Gilly Barr31-Mar-12 2:45
Gilly Barr31-Mar-12 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.