title: Load data from database view using NHibernate
body: <p>I'm using NHibernate and want to load a single field from a database view into my model. Here's the situation:</p>
  
  <p>I have one USERS table which has a column called AVATAR_ID. There's also a separate view defined in the database called VIEW_RESOURCES. This view contains an ID and a PATH column. The ID can be matched to the values stored in the AVATAR_ID column.</p>
  
  <p>In SQL I'd be doing the following:</p>
  
  <pre><code>select U.ID, U.USERNAME, R.PATH
  from USERS U
  left join VIEW_RESOURCES R on R.ID = U.AVATAR_ID
  where U.ID = ...
  </code></pre>
  
  <p>I've defined the properties of a model in my C# code as follows:</p>
  
  <pre><code>public virtual Id { get; protected set; }
  public virtual string UserName { get; set; }
  public virtual string AvatarPath { get; protected set; }
  </code></pre>
  
  <p>I'm using FluentNHibernate for the mapping, which looks like this:</p>
  
  <pre><code>Table("USERS");
  Id(x =&gt; x.Id).GeneratedBy.Identity().Column("ID");
  Map(x =&gt; x.UserName).Column("USERNAME").Not.Nullable().Length(256);
  </code></pre>
  
  <p>The code used for loading a single user looks like this:</p>
  
  <pre><code>User user = session
  .QueryOver&lt;User&gt;()
  .Where(x =&gt; x.Id == id)
  .SingleOrDefault();
  </code></pre>
  
  <p>So far so good, but what I need to know is:</p>
  
  <ul>
  <li>How to define the mapping for the AvatarPath?</li>
  <li>How to load the PATH column of the VIEW_RESOURCES view into the AvatarPath property of my User object? I'd rather not load it as a separate model which has a relationship to the user. I want it as a property on User, if that's possible.</li>
  </ul>