I am posting this to nh-dev instead of nh-users since I got my code
working, and am just wondering if some NH code needs corrected or not.
I mapped a System.Uri property called "URL" to an nvarchar column
using the "UriType" IUserType posted here:
http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx
I then created an ICriteria query:
return session.CreateCriteria<Bookmark>()
.Add( Expression.Like("URL", "file:%" )
.List<Bookmark>();
To my surprise, the above List() call causes an exception to be
thrown:
NHibernate.QueryException was unhandled by user code
Message=Type mismatch in NHibernate.Criterion.SimpleExpression: URL
expected type System.Uri, actual type System.String
Source=NHibernate
StackTrace:
at
NHibernate.Criterion.CriterionUtil.GetColumnNamesUsingPropertyName(ICriteriaQuery
criteriaQuery, ICriteria criteria, String propertyName, Object value,
ICriterion critertion)
at
NHibernate.Criterion.CriterionUtil.GetColumnNamesForSimpleExpression(String
propertyName, IProjection projection, ICriteriaQuery criteriaQuery,
ICriteria criteria, IDictionary`2 enabledFilters, ICriterion
criterion, Object value)
at NHibernate.Criterion.SimpleExpression.ToSqlString(ICriteria
criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters)
at
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2
enabledFilters)
at
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable
persister, CriteriaQueryTranslator translator,
ISessionFactoryImplementor factory, ICriteria criteria, String
rootEntityName, IDictionary`2 enabledFilters)
at
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable
persister, ISessionFactoryImplementor factory, CriteriaImpl
rootCriteria, String rootEntityName, IDictionary`2 enabledFilters)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria,
IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List[T]()
The NH source code that threw the exception looks like this:
if (value != null && !(value is System.Type) && !
propertyType.ReturnedClass.IsInstanceOfType(value))
{
throw new QueryException(string.Format(
"Type mismatch in {0}: {1} expected type {2}, actual type {3}",
critertion.GetType(), propertyName, propertyType.ReturnedClass,
value.GetType()));
}
I got my code working by changing it like this:
return session.CreateCriteria<Bookmark>()
.Add( Expression.Like("URL", new Uri("file:%", UriKind.Relative)) )
.List<Bookmark>();
But I'm thinking that maybe NH should not have thrown that exception
to begin with. Would anyone else support this idea? Or, if it is
working exactly like it should, then please explain.