tags:

views:

655

answers:

1

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? Thanks.

+2  A: 

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

You are awesome, worked! I'm not sure how I missed that. I figured there had to be a way to help HQL figure out what to do.
eyston
This behavior changed to "do the right thing" (act like Criteria and call the IUserType) in NH 2.1.2, but your tip solves my problem for 2.1.0 (which I'm stuck on for various reasons). Thanks for this!
Nicholas Piasecki