NHibernate 2.0.1 Criteria: Throwing IsNull Exception Exception

I have used NHibernate 2.0.1.4000

.NET 3.5 SP1 for all current projects and had no problem with any other requests (using the Query or Criteria API) until some new business logic asked the need for a new request in this particular project with an application database that should retrieve records with a specific null value (of type DateTime) from one table.

Knowing that you cannot use an unequal constraint for this query type, but that you can use the IsNull constraint instead, this simple query generates "The value cannot be null!" Execution exception. I have extensive log4net log files in DEBUG mode that I have looked through and have not helped yet, and I have confirmed that my class for the table indicates that the property I am checking is nullable (DateTime?), To avoid the problem that can be called by forcing the update of records, etc., which is not happening here ...

Here's the query, nothing too complicated, and I've tried with / without the additional MaxResults constraint to fix it as a problem, and yet every time an exception is thrown before I can collect the results:

ICriteria criteria = session.GetISession().CreateCriteria(typeof (Order)).Add(NHibernate.Criterion.Restrictions.IsNull("ShippedOn")).SetMaxResults(10);

IList<Order> entityList = criteria.List<Order>();

      

Any ideas or pointers for more information that might help me resolve this issue? I've tried using HQL as an alternative, same problems ... Am I missing something here regarding returning records with a specific null property?

+1


source to share


3 answers


Quick update ... but after further investigation it turns out that the Exception is actually thrown when the transaction and unit of work is complete and the session.Flush () method is called, and again has something to do with how NHibernate is trying to deal with a null value of a table field / DAO. Which even though I worked in my class and mapped for the applicable table, the actual SQL my constrained criterion generates, throws an exception to be thrown in Flush on the SqlDateTime issue ...



Currently my workaround was to get the records from my first constraint and handle the IsNull check in code instead of requesting. Not as performer, but until I figure out it works ...

0


source


I've seen this kind of exception before when there is a mismatch between your domain model, mapping and database. For example, if you have a null DateTime field in your database, but not a nullable property on your model.



I covered this in a blog post a while ago. I can't say for sure if this is your problem, but it certainly sounds familiar.

0


source


DateTimes are not nullable in .net. Have you tried changing your domain model to use DateTime? which is null?

You can get similar exceptions using query criteria. Suppose I have a Person domain object with a Name string property. I can plot the following criteria:

ICriteria criteria = session.CreateCriteria(typeof(Person)
.Add(Restrictions.Eq(1234))

      

however, I am listing this criterion. NHibernate will throw a type mismatch exception because I am testing string against int. Behind the scenes NHIbernate is doing some clever reflective type checking on mappings and will throw exceptions if the types don't line up. (you would probably get a SqlException if it wasn't that smart)

0


source







All Articles