-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: HQL WHERE with nullable date type
PostPosted: Fri Jul 08, 2005 2:46 pm 
Newbie

Joined: Fri Jul 08, 2005 2:25 pm
Posts: 3
Location: San Francisco, CA
Hi there,

I have an object called Product with a property called ReleaseDate. ReleaseDate is a nullable DateTime. I want to retrieve all the Product objects whose ReleaseDate occurs in the future (i.e., greater than DateTime.Now).

How to go about this? I'm assuming I need to use HQL for this but I'm not sure how to put together the right combination of criteria. Currently I'm doing this:

Code:
ICriteria cri = session.CreateCriteria(typeof(Product.Product));
cri.Add(NHibernate.Expression.Expression.IsNotNull("ReleaseDate"));


This works as expected (and returns those products whose ReleaseDate is not null); now how do I add a second criteria that says "give me only those Products whose ReleaseDate is greater than the current date?"

TIA,

Jeffrey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 3:04 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
have you tried:

Code:
return session.CreateCriteria(typeof(Product.Product))
    .Add(Expression.IsNotNull("ReleaseDate"))
    .Add(Expression.Gt("ReleaseDate", DateTime.Now))
    .AddOrder(Order.Asc("ReleaseDate"))
    .List();


-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 3:25 pm 
Newbie

Joined: Fri Jul 08, 2005 2:25 pm
Posts: 3
Location: San Francisco, CA
Yep, doesn't work ("NHibernate.ADOException : problem in find ----> System.InvalidCastException : Specified cast is not valid.")

I wonder if this is because it's trying to compare the ReleaseDate property (a NullableDateTime) to a DateTime (DateTime.Now)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 6:48 pm 
Newbie

Joined: Fri Jul 08, 2005 2:25 pm
Posts: 3
Location: San Francisco, CA
OK! Figured it out. Created an instance of Nullables.NullableDateTime and cast the value of DateTime.Now to it. My working code looks like this:

Code:
System.Collections.IList prods = null;
Nullables.NullableDateTime dt = null;
dt = (Nullables.NullableDateTime)DateTime.Now;

ICriteria cri = session.CreateCriteria(typeof(Product.Product));
cri.Add(NHibernate.Expression.Expression.IsNotNull("ReleaseDate"));
cri.Add(NHibernate.Expression.Expression.Gt("ReleaseDate", dt));         cri.AddOrder(NHibernate.Expression.Order.Asc("ReleaseDate"));
prods = cri.List();


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 09, 2005 1:47 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
sorry. should've seen that. wasn't paying attention..

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.