-->
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.  [ 4 posts ] 
Author Message
 Post subject: DateDiff and DetachedCriteria
PostPosted: Thu Aug 21, 2008 9:59 am 
Newbie

Joined: Mon Jul 21, 2008 3:01 pm
Posts: 7
I have read the manual, searched the faq's and web to no avail. If you can point me to a specific document regarding an answer please do. Thanks!

I want to use a DetachedCriteria for the following SQL statement. I don't want to use any HQL or SQL.

Code:
select *
from [AnyTable]
where DateDiff(dd,[AnyDateField],getdate()) >[SomeIntegerValue]



Simply I want to tell if the number of days between today and a date field is greater then some value.

NOTES:
-- I'm using Nhibernate 1.2.1 GA.
-- I'm using SQL Server 2005.
-- Visual Studio 2005

Thanks -


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 12:14 pm 
Newbie

Joined: Mon Jul 21, 2008 3:01 pm
Posts: 7
I decided to do the following approach. If anyone has a better solution let me know. Thanks!

In my hibernate mappng file I created a new property..

Code:
<property name="DaysCheckedOut" formula="DateDiff(dd,CheckedOutOn,getdate())" />


Using the DetachedCriteria now yields a statement like..

Code:
detachedCriteria.Add(new NHibernate.Expression.GtExpression("DaysCheckedOut", 50));


The only problem with my approach is I really only need the DaysCheckedOut property when a user filters by this field. Since I made it a class property it will now always be part of the query when retrieving data. Not a huge performance penalty but I'd rather not always pull the field.

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 1:04 pm 
Newbie

Joined: Mon Jul 21, 2008 3:01 pm
Posts: 7
My solution does not work as NHibernate is trying to replace the 'dd' in the DateDiff function withe a field name from my table. Will continue to research just didn't want to lead anyone down the wrong path.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 2:28 pm 
Newbie

Joined: Mon Jul 21, 2008 3:01 pm
Posts: 7
ok, figured out a solution. I'm not overly thrilled with my solution but based on my current level of experience here it is...


Code:
dc.Add(Expression.Sql("(DateDiff(dd,CheckedOutOn,getdate()) >=" + rFilter.FieldValue + ")"));


Where dc is an instance of a DetachedCriteria object and CheckedOutOn is a field from my database.

The reason i'm not overly excited about this solution is i'm hard-coding SQL when I really want to use the criteria API.

So I did not add a new field to my xml mapping file or my class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.