问题
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