NHibernate: HQL and UserTypes as query parameters

拜拜、爱过 提交于 2020-01-13 04:11:51

问题


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.

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.

eg:

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>();

The above blows up because the query on shipment.ShipmentDate ends up being '4/28/2009 12:00:00' instead of the UserType format.

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>();

Everything works fine because the date is translated using the UserType for shipment.ShipmentDate.

Am I missing something to hint at HQL what to do?


回答1:


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))



来源:https://stackoverflow.com/questions/799457/nhibernate-hql-and-usertypes-as-query-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!