title: How do I create a Fluent nHibernate component mapping with data from another table
body: <p>I'm running into some difficulty setting up a Fluent nHibernate mapping where I'm trying to use the Component function to build up an object that with data from two related database tables.</p>
  
  <p>Below is a simplified version of the database and code that outlines the basic problem I'm having.</p>
  
  <p>I've got a database setup that looks similar to the following:</p>
  
  <pre><code>Account
    - AccountId INT (PRIMARY KEY)
    - Currency VARCHAR(3)
  
  SubAccount
    - SubAccountId INT (PRIMARY KEY)
    - AccountId INT (FOREIGN KEY reference to Account)
    - Balance DECIMAL
  </code></pre>
  
  <p>I'm trying to map this data into the following classes:</p>
  
  <pre><code>public class Account
  {
      public virtual int Id { get; private set; }
      public virtual string Currency { get; private set; }
  }
  
  public class SubAccount
  {
      public virtual int Id { get; private set; }
      public virtual Account Account { get; private set; }
      public virtual Money Balance { get; private set; }
  }
  </code></pre>
  
  <p>The <code>Money</code> class looks like:</p>
  
  <pre><code>public class Money
  {
      public decimal Value { get; private set; }
      public string Currency { get; private set; }
  }
  </code></pre>
  
  <p>The part I'm struggling with is where I'm trying to map the <em>SubAccount.Balance</em> and <em>Account.Currency</em> values into the <code>SubAccount.Balance</code> <code>Money</code> object.</p>
  
  <p>My mapping classes (below) do the majority of the work but I can't figure out how to build up the <code>Balance</code> object using the <em>Balance</em> value from the <em>SubAccount</em> table and the <em>Currency</em> value from the <em>Account</em> table.</p>
  
  <pre><code>public class AccountMap : ClassMap&lt;Account&gt;
  {
      public AccountMap()
      {
          Table("Account");
  
          Id(x =&gt; x.Id)
              .Column("AccountId");
          Map(x =&gt; x.Currency)
              .Column("Currency");
      }
  }
  
  public class SubAccountMap : ClassMap&lt;SubAccount&gt;
  {
      public SubAccountMap()
      {
          Table("SubAccount");
  
          Id(x =&gt; x.Id)
              .Column("AccountId");
          References(x =&gt; x.Account)
              .Column("AccountId");
  
          Component(x =&gt; x.Balance,
              m =&gt;
              {
                  m.Map(y =&gt; y.Value)
                      .Column("Balance");
  
                  m.Map(y =&gt; y.Currency)
                      .Column("Account.Currency"); // &lt;-- This is where I get stuck
              });
      }
  }
  </code></pre>
  
  <p>Is what I'm trying to do at all possible using Fluent nHibernate?</p>