Results 1 to 3 of 3

Thread: using date functions in hibernate criteria restrictions (non-HQL)

  1. #1
    Join Date
    Mar 2007
    Posts
    27

    Default using date functions in hibernate criteria restrictions (non-HQL)

    Hello all,

    I have a DetachedCriteria on which I am trying to add a restriction. Essentially I want the following SQL where clause:

    Code:
    WHERE MONTH(someDate) = <some month integer value>
    -- but I can't seem to find the proper syntax for this restriction:

    Code:
    crit.add(Restrictions.eq("month(someDate)", cal.get(GregorianCalendar.MONTH)));
    I've seen sqlRestriction which I think I could use to solve this problem, but I'm interested to learn how (if possible) this can be used with the eq restriction.

    Thanks
    - Bryan M.

  2. #2

    Default

    hmm maybe try

    Code:
    WHERE ? between '1/01/07' AND '2/01/07'
    
    
    in criteria speak,
    
        criteria.add(Expression.and(
                Expression.gte(date, dateUtil.firstOfMonth(date)),
                Expression.lt(date, dateUtil.firstOfNextMonth(date)));
    I fully encourage you to come up with your own DateUtil bean for operations like this. The calendar API sucks, it is very nice to put all date manipulations in one place. Our dateUtil has become very important and has about 30 or so useful operations.

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    As lloyd said doing a between should do the job, the problem is getting access to the date your actually trying to query on. It's quite an interesting problem though, can't say I've thought of this before.
    Last edited by karldmoore; Aug 29th, 2007 at 12:12 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •