问题描述
我正在使用一个日期格式很奇怪的数据库.我写了一个UserType来将标准.NET DateTime传输到/从怪异的格式传输,并且工作正常.
I'm using a database that has a weird date format. I wrote a UserType to transfer standard .NET DateTime to/from the weird format and it works fine.
我通常使用ICriteria查询,但决定在此项目上尝试使用HQL进行IQuery.我遇到一个问题,查询无法将参数转换为适当的UserType.
I've normally used ICriteria queries but decided to try IQuery using HQL on this project. I ran into a problem that query doesn't translate parameters to the appropriate UserType.
例如:
IQuery query = session.CreateQuery("from OfflineShipmentLineItem as line join fetch line.Shipment as shipment join fetch line.Extension where shipment.ShipmentDate = :date");
query.SetParameter("date", date);
return query.List<OfflineShipmentLineItem>();
以上内容之所以炸毁,是因为关于运送的查询.ShipmentDate最终是'4/28/2009 12:00:00'而不是UserType格式.
The above blows up because the query on shipment.ShipmentDate ends up being '4/28/2009 12:00:00' instead of the UserType format.
如果我改用ICriteria,则效果很好:
If I instead use ICriteria it works fine:
ICriteria criteria = session.CreateCriteria(typeof(OfflineShipmentLineItem));
criteria.SetFetchMode("Shipment", FetchMode.Eager);
criteria.SetFetchMode("Extension", FetchMode.Eager);
criteria.CreateAlias("Shipment", "shipment");
criteria.Add(Expression.Eq("shipment.ShipmentDate", date));
return criteria.List<OfflineShipmentLineItem>();
一切正常,因为日期是使用UserType进行翻译的,用于装运.ShipmentDate.
Everything works fine because the date is translated using the UserType for shipment.ShipmentDate.
我是否想暗示HQL该怎么做?
Am I missing something to hint at HQL what to do?
推荐答案
我没有时间尝试自己,但还尝试添加SetParameter的第三个参数(即IType).作为参数使用NHUtils.Custom(typeof(YourIUserType))
i don't have time to try myself but try to add also the 3rd arguments of SetParameter (the IType).As parameter use NHUtils.Custom(typeof(YourIUserType))
这篇关于NHibernate:HQL和UserTypes作为查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!