There is not so much difference in mapping between a table and a View in NHibernate. Since view is generally considered a virtual table with read only mode. Some properties needs to be changed in the hbml.xml file and a persistence class.
For clarifying the concept here is my table definition consisting two tables Milestones and MilestoneCompletions.


From the above two table I have created a View “ViewCompletedMilestone” which is responsible for selecting only completed milestones.
SQL for View
SELECT dbo.MilestoneCompletions.CompletionDate, dbo.Milestones.MilestoneID, dbo.Milestones.ProjectID, dbo.Milestones.MemberID, dbo.Milestones.Name, dbo.Milestones.Description, dbo.Milestones.DueDate, dbo.Milestones.CreatedDate, dbo.Milestones.UpdatedDate FROM dbo.MilestoneCompletions INNER JOIN dbo.Milestones ON dbo.MilestoneCompletions.MilestoneID = dbo.Milestones.MilestoneID
Persistence Class - ViewCompletedMilestone.cs
namespace DataTransfer
{
public class ViewCompletedMilestone
{
private Guid _milestoneId;
public virtual Guid MilestoneId
{
get { return _milestoneId; }
private set { _milestoneId = value; }
}
private DateTime _CompletedDate;
public virtual DateTime CompletionDate
{
get { return _CompletedDate; }
private set { _CompletedDate = value; }
}
private Guid _ProjectID;
public virtual Guid ProjectID
{
get { return _ProjectID; }
private set { _ProjectID = value; }
}
private int _MemberID;
public virtual int MemberID
{
get { return _MemberID; }
private set { _MemberID = value; }
}
private string _Name;
public virtual string Name
{
get { return _Name; }
private set { _Name = value; }
}
private string _Description;
public virtual string Description
{
get { return _Description; }
private set { _Description = value; }
}
private DateTime _DueDate;
public virtual DateTime DueDate
{
get { return _DueDate; }
private set { _DueDate = value; }
}
private DateTime _CreatedDate;
public virtual DateTime CreatedDate
{
get { return _CreatedDate; }
private set { _CreatedDate = value; }
}
private DateTime _UpdatedDate;
public virtual DateTime UpdatedDate
{
get { return _UpdatedDate; }
private set { _UpdatedDate = value; }
}
}
}
Since we are not going to change the data from our persistence class, setters are declared as private.
Hibernate Mapping File - ViewCompletedMilestone.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
<class name="DataTransfer.ViewCompletedMilestone" table="ViewCompletedMilestone" mutable="false">
<id name="MilestoneId" column="MilestoneId" type="Guid">
<generator class="guid" />
</id>
<property name="CompletionDate" column="CompletionDate" type="DateTime" not-null="true" />
<property name="ProjectID" column="ProjectID" type="Guid" not-null="true" />
<property name="MemberID" column="MemberID" type="Int32" not-null="true" />
<property name="Name" column="MemberID" type="string" length="255" not-null="true" />
<property name="Description" column="Description" type="string" not-null="true" />
<property name="DueDate" column="DueDate" type="DateTime" not-null="true" />
<property name="CreatedDate" column="CreatedDate" type="DateTime" not-null="true" />
<property name="UpdatedDate" column="UpdatedDate" type="DateTime" not-null="true" />
</class>
</hibernate-mapping>
It should be noted that the mutable property of the class node is set as false because we don’t want our mapped class to make some changes.
Data Access Layer for mapped class - NHibernateDataProvider.cs
using NHibernate;
using NHibernate.Cfg;
using DataTransfer;
namespace DataAccessLayer
{
public class NHibernateDataProvider
{
private ISession _session;
public ISession Session
{
set { _session = value; }
}
public NHibernateDataProvider(ISession session)
{
_session = session;
}
public IList<ViewCompletedMilestone> GetCompletedMilestones()
{
return _session
.CreateCriteria(typeof(ViewCompletedMilestone))
.List<ViewCompletedMilestone>();
}
}
}
In order to check the result of our class we should do unit testing. I have used MBUnit for unit testing.
Unit Testing Class - DataAccessLayerTest.cs
using MbUnit.Framework;
using DataTransfer;
using DataAccessLayer;
using NHibernate;
namespace DataAccessLayerTest
{
[TestFixture]
public class DataAccessLayerTest
{
private NHibernateDataProvider _provider;
private NHibernateSessionManager _manager;
private ISession _session;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_manager = new NHibernateSessionManager();
}
[SetUp]
public void SetUp()
{
_session = _manager.GetSession();
_provider = new NHibernateDataProvider(_session);
}
[Test]
public void CanGetCompletedMilestones()
{
IList<ViewCompletedMilestone> milestones = _provider.GetCompletedMilestones();
foreach (ViewCompletedMilestone m in milestones)
{
Assert.GreaterThan(m.CompletionDate, m.CreatedDate);
}
}
}
}
Result of the unit test:

SQL generated by NHibernate in the console:
SELECT this_.MilestoneId as Mileston1_0_0_, this_.CompletionDate as Completi2_0_0_, this_.ProjectID as ProjectID0_0_, this_.MemberID as MemberID0_0_, this_.Description as Descript5_0_0_, this_.DueDate as DueDate0_0_, this_.CreatedDate as CreatedD7_0_0_, this_.UpdatedDate as UpdatedD8_0_0_ FROM ViewCompletedMilestone this_
Download this file here.