views:

649

answers:

2

I'm having trouble figuring out how to delete a set of records when a particular field is less than the date on the sql server without using tables or field names.

Since I'm using MSSQL the query would look something like this:

DELETE FROM tickets WHERE expires < getdate()

How would I go about getting Hibernate to do this? I'm looking into HQL but I don't see a way to specify getdate(). Help!

A: 

I believe you can use query.substitutions configuration. Take a look at this post and links in the answer:

http://stackoverflow.com/questions/435645/getting-datepart-in-hql-or-criteria/435720

bbmud
A: 

You should probably define a named SQL query:

<sql-query name="DeleteExpiredTickets">
 DELETE FROM tickets WHERE expires < getdate()
</sql-query>

Then call:

session.getNamedQuery("DeleteExpiredTickets").executeUpdate();
James L
I may do it this way. Where does this go in the config file?
Spencer Ruport
If you're using hbms you need to add it in the same way as a class mapping. Just replace the class definition with the code above and you're set.
James L