Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4023060
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:33:09+00:00 2026-05-20T10:33:09+00:00

I recently got started with NHibernate and am having some trouble implementing the domain

  • 0

I recently got started with NHibernate and am having some trouble implementing the domain model outlined further down.

What I’m looking for is a way to filter the relationship between an Item and it’s ItemData collection on specific DataStores. DataStores are either global, in which case they are always returned, or specific to a user identity (based on application instance).

In SQL this can be accomplished using a simple query:

SELECT * FROM Items i
INNER JOIN ItemData id ON (i.ItemId=id.ItemId)
LEFT OUTER JOIN Users u ON (id.UserId=u.UserId)
LEFT OUTER JOIN DataStore ds ON (id.DataStoreId=ds.DataStoreId)
WHERE ds.IsGlobal = 1 OR ds.UserId = @userId

Database structure:

DataStore:
- DataStoreId (PK)
- Name
- Weight
- UserId
- IsGlobal

Item:
- ItemId (PK)
- ... (non-nullable fields)

ItemData:
- ItemDataId (PK)
- ItemId
- DataStoreId
- ... (nullable fields)

Domain model:

public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Id(x => x.Id, "ItemId");
        HasMany(x => x.Data)
            .KeyColumn("ItemId")
            .ApplyFilter<ItemDataFilter>(..?)
            .Cascade.AllDeleteOrphan();
    }
}

The basic theory is to fetch one ItemData row per DataStore and join each column on the weight field of the respective DataStore (first non-null value ordered by weight).

Insight as to if and how this could be accomplished in NHibernate would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:33:10+00:00Added an answer on May 20, 2026 at 10:33 am

    Heres what I’ve found myself in case anyone else is looking for this information.

    1.Create a custom filter:

    public class ItemDataFilter : FilterDefinition
    {
        public ItemDataFilter()
        {
            WithName("ItemDataFilter").WithCondition("Data.DataStoreId == :DataStoreId").AddParameter("DataStoreId", NHibernate.NHibernateUtil.Int32);
        }
    }
    

    2.Modify your Fluent NHibernate property mapping (with .ApplyFilter<>()):

    HasMany(x => x.Data)
        .KeyColumn("ItemId")
        .ApplyFilter<ItemDataFilter>()
        .Cascade.AllDeleteOrphan();
    

    3.In your repository enable the filter and set it’s property for the current session:

    public IList<Item> GetItemsByDataStore(int DataStoreId)
        {
        using (var session = NHibernateHelper.OpenSession())
        {
            session.EnableFilter("ItemDataFilter").SetParameter("DataStoreId", DataStoreId);
            return session.CreateCriteria(typeof(Item)).List<Item>();
        }
    }
    

    Another way to do this would be to fetch all ItemData for each Item and add another non-mapped property that does this filtering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I've started to develop Android Soft Keyboard and got some problem with preferences.
I recently got started trying to use SFML. For some reason my simple program
I recently got started trying to use SFML. For some reason my simple program
I've got build server running CruiseControl.NET and recently it started throwing this error on
I got started with threads Thread.Start() recently and Long Running Tasks and noticed several
Recently got started on MapGuide. Kind of lost. The amount of information available on
I have recently started playing around with iOS development and have got most of
I recently started solving Project Euler problems in Scala, however when I got to
I've just recently started learning/using Ruby at work. I've got a super-simple test script
I've recently started checking out XNA. All is going well and I've even got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.