Nhibernate: Stop it from joining to a table that is not needed

Posted by Aaron on Stack Overflow See other posts from Stack Overflow or by Aaron
Published on 2010-06-08T18:46:21Z Indexed on 2010/06/08 18:52 UTC
Read the original article Hit count: 290

I have two tables (tbArea, tbPost) that relate to the following classes.

class Area
{
    int ID
    string Name
    ...
}

class Post
{
    int ID
    string Title
    Area Area
    ...
}

These two classes map up with Fluent Nhibernate. Below is the post mapping.

public class PostMapping : ClassMap<Post>
{
    public PostMapping()
    {
        Cache.NonStrictReadWrite();

        this.Table("tbPost");

        Id(x => x.ID)
            .Column("PostID")
            .GeneratedBy
            .Identity();

        References(x => x.Area)
            .ForeignKey("AreaID")
            .Column("AreaID");
        ...
    }
}

Any time I perform a query on the Post table "where AreaID = 1(any AreaId)", nhibernate will join to the area table.

(What Nhibernate generates for a query)

SELECT
    post fields
,   area fields (automatically added)
FROM tbPost p
LEFT JOIN tbArea a on 
        p.areaid = a.areaid
where
    p.areaid = 1

I have tried setting Area to LazyLoad, to Fetch.Select, ReadOnly, and any other setting on the reference and still it will always join to Area.

I am trying to optimize the backend database queries, and since I don't need the area object loaded just filtered I would like to eliminate the unnecessary join to Area each time I Query post.

What configurations do I need to change or mappings to get area to still be related to post in my objects, but not query it when I filter on AreaID?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate