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

  • SEARCH
  • Home
  • 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 6902571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:48:52+00:00 2026-05-27T07:48:52+00:00

Short version: Is it possible in NHibernate to create a mapping for a class

  • 0

Short version:
Is it possible in NHibernate to create a mapping for a class that has no corresponding table in the database and specify for each property where the data should come from?


Long version:

I have the following class:

public class TaxAuditorSettings
{
    private readonly IList<Month> _allowedMonths = new List<Month>();
    private readonly IList<Company> _allowedVgs = new List<Company>();

    public IList<Month> AllowedMonths
    {
        get { return _allowedMonths; }
    }

    public IList<Company> AllowedVgs
    {
        get { return _allowedVgs; }
    }
}

The class Company is a normal entity that is mapped to a table.
The class Month is a simple class without ID or existing mapping (Constructor and error checking removed for brevity):

public class Month
{
    public int MonthNumber { get; set; }
    public int Year { get; set; }
}

My database has the following two tables:

Table TAX_AUDITOR_ALLOWED_COMPANIES has only one column COMPANY_ID that is a FK to the table COMPANY and has a UNIQUE index.

Table TAX_AUDITOR_ALLOWED_MONTHS has two columns MONTH_NUMBER and YEAR. There is a UNIQUE index spanning both columns.

I would like to map TaxAuditorSettings such that I can ask my NHibernate session for an object of this type and NHibernate then should put the contents of TAX_AUDITOR_ALLOWED_MONTHS into the list TaxAuditorSettings.AllowedMonths and the companies referenced in TAX_AUDITOR_ALLOWED_COMPANIES into the list TaxAuditorSettings.AllowedCompanies.

Is this even possible? If so, how? If not, how would you do it instead?

Please note: I can change the database if necessary.

  • 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-27T07:48:53+00:00Added an answer on May 27, 2026 at 7:48 am

    not quite what you requested for but here goes

    public TaxAuditorSettings GetAuditorSettings(ISession session)
    {
        // assuming there is a ctor taking the enumerables as parameter
        return new TaxAuditorSettings(
            session.CreateSQLQuery("SELECT MONTH_NUMBER, YEAR FROM TAX_AUDITOR_ALLOWED_MONTHS")
                .SetResultTransformer(new MonthResultTransformer())
                .Future<Month>(),
            session.CreateCriteria<Company>()
                .Add(NHibernate.Criterion.Expression.Sql("Id IN (SELECT COMPANY_ID FROM TAX_AUDITOR_ALLOWED_COMPANIES)"))
                .Future<Company>())
    }
    
    class MonthResultTransformer : IResultTransformer
    {
        public IList TransformList(IList collection)
        {
            return collection;
        }
    
        public object TransformTuple(object[] tuple, string[] aliases)
        {
            return new Month
            {
                MonthNumber = (int)tuple[0],
                Year = (int)tuple[1],
            }
        }
    }
    

    Update: saving

    public void SaveOrUpdate(ISession session, TaxAuditorSettings settings)
    {
        using (var tx = session.BeginTransaction())
        {
            // get whats in the database first because we dont have change tracking
            var enabledIds = session
                .CreateSqlQuery("SELECT * FROM TAX_AUDITOR_ALLOWED_COMPANIES")
                .Future<int>();
    
            var savedMonths = session
                .CreateSQLQuery("SELECT MONTH_NUMBER, YEAR FROM TAX_AUDITOR_ALLOWED_MONTHS")
                .SetResultTransformer(new MonthResultTransformer())
                .Future<Month>();
    
            foreach (var id in settings.AllowedVgs.Except(enabledIds))
            {
                session.CreateSqlQuery("INSERT INTO TAX_AUDITOR_ALLOWED_COMPANIES Values (:Id)")
                    .SetParameter("id", id).ExecuteUpdate();
            }
    
            foreach (var month in settings.AllowedMonths.Except(savedMonths))
            {
                session.CreateSqlQuery("INSERT INTO TAX_AUDITOR_ALLOWED_MONTHS Values (:number, :year)")
                    .SetParameter("number", month.Number)
                    .SetParameter("year", month.Year)
                    .ExecuteUpdate();
            }
    
            tx.Commit();
        }
    }
    

    Note: if you can change the database it would be much easier and performant to sanitise the tables

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

Sidebar

Related Questions

Short version : Is it possible or not to use impersonation in ASP.NET to
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
Short version: I'm wondering if it's possible, and how best, to utilise CPU specific
Short version: How can I map two columns from table A and B if
Possible Duplicate: Getting a value from HttpServletRequest.getRemoteUser() in Tomcat without modifying application Short version:
Short version : VB.Net Windows forms feature controls that are often dragged from the
is it possible to implement short urls in asp.net 2.0 version. please give me
Possible Duplicate: How to enable PHP short tags ? I am using php version
Short Version Are class variables thread-safe for the duration of a controller action? Long
Short version Need to distribute nightly builds to 70+ people each morning, would like

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.