views:

35

answers:

2

I'm trying to restrict my NHibernate query with some basic date/time manipulation. More specifically, I want to execute the following statement (pseudo-SQL):

select * from article where created_on + lifespan >= sysdate

with:

  • created_on is mapped to a property of type DateTime.
  • lifespan is mapped to a property of type TimeSpan.
  • sysdate is the current date/time (of the database server or ofthe application host, I don't care)

Is there any built-in way to do that by using the Criteria-API or HQL?

return session
  .CreateCriteria<Article>()
  .Add( ? )
  .List<Article>();
+1  A: 

Since the queries are executed by the server, it needs to support the operations you want to do.

If it does, you'd need to inherit the corresponding Dialect and register the corresponding functions in its constructor.

Diego Mijelshon
+1 Very good reusable proposal that emphasizes NHibernate's extensibility. I guess it's as close as possible to what I was looking for. Thank you.
Yann Trevin
+1  A: 

create view activearticle as select * from article where created_on + lifespan >= sysdate

Mike Stockdale
+1. Creating a dedicated view is a good solution.
Yann Trevin