I was working on a very simple example to test views on NHibernate. However I couldn't get the thing to work. The code compiles, the program runs, the call to Configure() from the configuration object succedes and I can create instances of the object representing the view.
But when I try to list the content of the view the result is always an empty List. I know the view returns values because I tested it on sql server, I'm not doing any writing, deleting or updating records and the source of data from the view is from a single table.
I can't see what I'm doing wrong, maybe its something stupid but I just can't figure it out. Anyway, this is what I've got:
Database:
Table Customer(id, name)
View CustomerTypes(id, name) -> a simple SELECT * FROM CUSTOMER
The Mappings:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest">
<class name="Customer" table="Customer">
<id name="Id" column="Id" type="Int64">
<generator class="native"/>
</id>
<property column="Name" type="String" name="Name" length="50"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.CustomerTypes,NHibernateTest" table="CustomerTypes" lazy="true" mutable="false">
<property type="long" access="field" not-null="true" name="_id" column="[id]" />
<property type="string" access="field" not-null="true" length="50" name="_name" column="[name]" />
</class>
</hibernate-mapping>
The Classes:Code:
public class Customer
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
public class CustomerTypes
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
The program code:Code:
Configuration cfg = new Configuration();
cfg.Configure();
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
IList l = new List<object>();
CustomerTypes ct = new CustomerTypes();
ICriteria crit = session.CreateCriteria(typeof(CustomerTypes));
crit.List(l);
transaction.Commit();
session.Close();
The list comes out empty, with 0 elements.
Can anyone see a the problem? The Customer class works fine, I can list its content, but the view doesn't work. :(
Thanks in advance.