Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategies for Mapping Views in NHibernate

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?

Edit
Answer So Far

Mapping

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

Class

 public class ProductView
    {
        public virtual Name {get; set;}
        //more properties
    }

View SQL
Do I still need NewID()?

Select NewID() as Id, ProductName as Name, --More columns
From Product  
like image 402
Nathan Fisher Avatar asked Jun 03 '10 02:06

Nathan Fisher


People also ask

What inheritance mapping strategies does NHibernate support?

Inheritance Mapping 9.1. The Three Strategies NHibernate supports the three basic inheritance mapping strategies: In addition, NHibernate supports a fourth, slightly different kind of polymorphism: It is possible to use different mapping strategies for different branches of the same inheritance hierarchy.

What is the mapping language in NHibernate?

The mapping language is object-centric, meaning that mappings are constructed around persistent class declarations, not table declarations. Note that, even though many NHibernate users choose to define XML mappings by hand, a number of tools exist to generate the mapping document, even transparently at runtime.

What is NHibernate entity-name?

entity-name (optional - defaults to the class name): NHibernate allows a class to be mapped multiple times, potentially to different tables. See Section 5.3, “Mapping a class more than once” . It also allows entity mappings that are represented by dictionaries at the .Net level.

What does the unique attribute do in NHibernate?

The unique attribute controls NHibernate's DDL generation with the SchemaExport tool. Then the mapping for OrderItem might use: This is not encouraged, however.


2 Answers

You can make it just a little bit cleaner by not mapping the Id to a property and omitting the generator:

<id column="Id" type="guid"/>

That way, you keep the problem in the data layer, without leaking the implementation detail to your domain.

like image 73
Diego Mijelshon Avatar answered Oct 07 '22 00:10

Diego Mijelshon


As far as I know, NHibernate will require either an id or a composite-id definition since it's the mechanism by which it uniquely identifies a given record. If there is no combination of columns that provides a key for each row in the view, I think you are stuck with hacky workarounds.

like image 29
Ben Hoffstein Avatar answered Oct 06 '22 23:10

Ben Hoffstein