0
votes

This method gets called.

    public IList<MyStuff> GetMyStuff(Int64 MyStuffId)
    {
        ICriteria criteria = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof(MyStuff));
        criteria.Add(Expression.Eq("x", MyStuff));
        return criteria.List<MyStuff>();
    }

But if I profile SQL Server, I can see that NHibernate doesn't try to access the server.

No errors are thrown. It is just the criteria.List() simply returns 0 rows.

MyStuff is a class

public class MyStuff {
    public virtual int Id { get; set; }
    public virtual int x { get; set; }
    ... more attributes ....
    public override int GetHashCode() {
       return (GetType().FullName + "|" + Id.ToString()).GetHashCode();
    }
}

And MyStuff is a HBM mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" assembly="MyStuff" namespace="My.Stuff" default-lazy="false">
  <class name ="MyStuff" table="dbo.viewMyStuff" dynamic-update="false" lazy="false">
    <cache usage="read-only"/>
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="x" />
    .... other properties
  </class>
</hibernate-mapping>

The following works just:

select * from viewMyStuff

NHibernate does just fine with other classes/views in the same project.

In fact if I intentionally typo the "table" in the HBM file to "XviewXMyStuffX" NHibernate doesn't have any problem with the typo. Why is NHibernate simply ignoring the expected attempt to access my database view?

2

2 Answers

0
votes

I turns out that the view treats the attribute "x" as a string. But in nHibernate I define it as a Int64. These type differences much be causing the criteria to fail. But without any reported error?

0
votes

Double check that your query really tries to use the exact class you intenden, and that the mapping also applies to exactly the same class. Beware of classes with same name in different namespace or assembly, for instance. One cause of this type of issue is if you attempt to query for a class that is in fact not mapped in NHibernate - then NHibernate will return en empty result, and not an error.

Oh, and have you tried without the cache-element to rule that out?