Strategies for Mapping Views in NHibernate

Posted by Nathan Fisher on Stack Overflow See other posts from Stack Overflow or by Nathan Fisher
Published on 2010-06-03T02:44:47Z Indexed on 2010/06/03 3:04 UTC
Read the original article Hit count: 262

It seems that NHibernate needs to have an id tag specified as part of the mapping. This presents a problem for views as most of the time (in my experience) a view will not have an Id. I have mapped views before in nhibernate, but they way I did it seemed to be be messy to me.

Here is a contrived example of how I am doing it currently.

Mapping

  <class name="ProductView" table="viewProduct" mutable="false" >
    <id name="Id" type="Guid">
      <generator class="guid.comb" />
    </id>
    <property name="Name" />
<!-- more properties -->
  </class>

View SQL

Select NewID() as Id, ProductName as Name, --More columns
From Product  

Class

public class ProductView
{
    public virtual Id {get; set;}
    public virtual Name {get; set;}
}

I don't need an Id for the product or in the case of some views I may not have an id for the view, depending on if I have control over the View

Is there a better way of mapping views to objects in nhibernate?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping