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 6127639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:32:03+00:00 2026-05-23T16:32:03+00:00

I have two tables in a legacy database that I need to map using

  • 0

I have two tables in a legacy database that I need to map using NHibernate. Unfortunately, one of these tables are using a composite key and I am running into a problem which I describe below.

Firstly, here is the schema of the two tables:

CREATE TABLE [dbo].[tBenchmarkFxHedgeHistory](
    [BenchmarkFxHedgeHistoryId] [int] IDENTITY(1,1) NOT NULL,
    [BenchmarkFxHedgeId] [int] NOT NULL,
    [ModelId] [int] NOT NULL,
    [BaseCurrencyCode] [nvarchar](5) NOT NULL,
    [BenchmarkFxHedgeTypeId] [int] NOT NULL,
    [DateFrom] [smalldatetime] NOT NULL,
    [DateTo] [smalldatetime] NULL,
    [PctHedgeBackToBase] [decimal](13, 10) NULL,
    [Enabled] [bit] NOT NULL,
    [BenchmarkHedgeStatusId] [int] NOT NULL,
    [AuditActionId] [int] NOT NULL,
    [Timestamp] [timestamp] NOT NULL,
    [HistoryUser] [nvarchar](50) NOT NULL CONSTRAINT [DF_tBenchmarkFxHedgeHistory_HistoryUser]  DEFAULT (suser_sname()),
    [HistoryDate] [datetime] NOT NULL CONSTRAINT [DF_tBenchmarkFxHedgeHistory_HistoryDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_tBenchmarkFxHedgeHistory] PRIMARY KEY CLUSTERED 
(
    [BenchmarkFxHedgeHistoryId] ASC
)

CREATE TABLE [dbo].[tBenchmarkFxHedgeRatio](
    [BenchMarkFxHedgeId] [int] NOT NULL,
    [NonBaseCurrencyCode] [nvarchar](5) NOT NULL,
    [PctHedgeBackToBase] [decimal](13, 10) NOT NULL,
 CONSTRAINT [PK_tBenchmarkFxHedgeRatio] PRIMARY KEY CLUSTERED 
(
    [BenchMarkFxHedgeId] ASC,
    [NonBaseCurrencyCode] ASC
)

And here are the domain classes:

public class BenchmarkFxHedgeRuleHistory 
{
    private IList<BenchmarkFxRuleRatioHistory> _percentages = new List<BenchmarkFxRuleRatioHistory>();

    public virtual int Id { get; set; }
    public virtual string BaseCurrencyCode { get; set; }
    public virtual DateTime DateFrom { get; set; }
    public virtual DateTime? DateTo { get; set; }
    public virtual decimal? Percentage { get; set; }
    public virtual bool Enabled { get; set; }
    public virtual byte[] Timestamp { get; set; }
    public virtual BenchmarkFxHedgeStatus Status { get; set; }
    public virtual BenchmarkFxHedgeType Strategy { get; set; }
    public virtual Model Model { get; set; }
    public virtual BenchmarkFxHedgeRule Rule { get; set; }
    public virtual AuditAction AuditAction { get; set; }

    public virtual IList<BenchmarkFxRuleRatioHistory> Percentages
    {
        get { return _percentages; }
    }
}

[Serializable]
public class BenchmarkFxRuleRatioHistory
{
    public virtual string NonBaseCurrencyCode { get; set; }
    public virtual decimal Percentage { get; set; }
    public virtual BenchmarkFxHedgeRuleHistory HistoryEntry { get; set; }

    public override bool Equals(object obj)
    {
        var rule = obj as BenchmarkFxRuleRatioHistory;
        if (rule == null) return false;
        return rule.HistoryEntry.Id == HistoryEntry.Id && NonBaseCurrencyCode == rule.NonBaseCurrencyCode;
    }

    public override int GetHashCode()
    {
        return NonBaseCurrencyCode.GetHashCode() ^ HistoryEntry.GetHashCode();
    }
}

And finally, here are the NHibernate mapping files:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Tests.DomainModel" assembly="Tests.DomainModel" xmlns="urn:nhibernate-mapping-2.2">
    <class name="BenchmarkFxHedgeRuleHistory" table="`tBenchmarkFxHedgeHistory`" schema="`dbo`">
        <id name="Id" access="property" column="`BenchmarkFxHedgeHistoryId`">
            <generator class="native" />
        </id>
    <many-to-one name="Rule" class="BenchmarkFxHedgeRule" column="`BenchmarkFxHedgeId`" not-null="true" fetch="select" />
        <property name="BaseCurrencyCode" type="String" column="`BaseCurrencyCode`" length="5" />
        <property name="DateFrom" type="DateTime" column="`DateFrom`" />
        <property name="DateTo" type="DateTime" column="`DateTo`" />
        <property name="Enabled" type="Boolean" column="`Enabled`" />
        <property name="Percentage" type="Decimal" column="`PctHedgeBackToBase`" />
        <property name="Timestamp" type="BinaryBlob" column="`Timestamp`" />
        <many-to-one name="Status" class="BenchmarkFxHedgeStatus" column="`BenchmarkHedgeStatusId`" not-null="true" fetch="join" />
    <many-to-one name="Strategy" class="BenchmarkFxHedgeType" column="`BenchmarkFxHedgeTypeId`" not-null="true" fetch="join" />
        <many-to-one name="Model" class="Model" column="`ModelId`" not-null="true" fetch="select" />
    <many-to-one name="AuditAction" class="AuditAction" column="`AuditActionId`" not-null="true" fetch="join" />
    <bag name="Percentages" fetch="join" access="readonly" inverse="true" lazy="false" table="tBenchmarkFxHedgeRatioHistory" cascade="all-delete-orphan" subselect="">
      <key column="`BenchmarkFxHedgeHistoryId`" />
      <one-to-many class="BenchmarkFxRuleRatioHistory" />
    </bag>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Tests.DomainModel" assembly="Tests.DomainModel" xmlns="urn:nhibernate-mapping-2.2">
    <class name="BenchmarkFxRuleRatioHistory" table="`tBenchmarkFxHedgeRatioHistory`" schema="`dbo`">
    <composite-id>
      <key-property name="NonBaseCurrencyCode" type="String" column="`NonBaseCurrencyCode`" />
      <key-many-to-one name="HistoryEntry" class="BenchmarkFxHedgeRuleHistory" column="`BenchmarkFxHedgeHistoryId`" />
    </composite-id>
    <property name="Percentage" type="decimal" column="`PctHedgeBackToBase`" />
    </class>
</hibernate-mapping>

Now I use these in the following code:

using(var session = DataMapperConfiguration.SessionFactory.OpenSession())
{
    var sessionRule = session.Get<BenchmarkFxHedgeRule>(id);
    var historyList = session.Query<BenchmarkFxHedgeRuleHistory>()
        .Where(x => x.Rule == sessionRule).ToList();
    Assert.AreEqual(2, historyList[0].Percentages.Count);
}

What happens is that sessionRule is correctly hydrated, but historyList does not have its Percentages property correctly hydrated. It keeps coming back as an empty list, but I expect it to have members in the list since there are matching rows in the database.

Any advice? What could I be doing wrong?

  • 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-23T16:32:04+00:00Added an answer on May 23, 2026 at 4:32 pm

    It turns out that the problem was that I declared the Percentages collection as an IList instead of an ICollection. Changing the declaration to ICollection<BenchmarkFxRuleRatioHistory> solves the problem.

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

Sidebar

Related Questions

I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have a legacy data base and a relation one-to-one between two tables. The
I have two tables inside a database. One stores unique userNames and a unique
I have a legacy database with a pretty evil design that I need to
I have two tables in my legacy database Purchases Id int(PK) name varchar(50) MasterAccount
I have two projects using legacy databases with no associations between the tables. In
I have two tables (A and G) in an Oracle database that can be
I have two tables in a legacy database... tblParentTable (int id, string specialIdentifier, ...)
In a legacy database I have to work with nested tables that are associated
I have two tables that are joined together. A has many B Normally you

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.