-->
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: JPA Criteria date/time operators
PostPosted: Fri Jul 22, 2011 5:22 pm 
Newbie

Joined: Fri Jul 22, 2011 5:04 pm
Posts: 8
I can't figure out how to do date operations using the JPA CriteriaBuilder API.
Basically, I have two date fields in the database, and I want to add a restriction where one date is within 7 days of the other. Here is the code snippet:
Code:
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Object> criteria = builder.createQuery();
        Root<MyEntity> root = criteria.from(MyEntity.class);
        Path<Date> date1 = root.get("date1");
        Path<Date> date2 = root.get("date2");
        // This line is a compile error because CriteriaBuilder.diff() only applies to Numbers:
        Expression<Date> date2MinusOneWeek = builder.diff(date2, 7);
        criteria.where(builder.greaterThan(date1, date2MinusOneWeek));


I don't see a way to do that subtraction, though, since the sum and difference functions in CriteriaBuilder only accept subclasses of Number.
I'm using postgres, so I really just need it to generate something like "WHERE date1 > date2 - 7" in the end.
Any ideas?


Top
 Profile  
 
 Post subject: Re: JPA Criteria date/time operators
PostPosted: Wed Jul 27, 2011 1:27 pm 
Newbie

Joined: Fri Jul 22, 2011 5:04 pm
Posts: 8
I wasn't able to find a solution for this, so I added a new column to the database to actually store date2 - 7 days, so I can use it in my criteria queries:
Code:
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Object> criteria = builder.createQuery();
        Root<MyEntity> root = criteria.from(MyEntity.class);
        Path<Date> date1 = root.get("date1");
        Path<Date> date2MinusOneWeek = root.get("date2MinusOneWeek");
        criteria.where(builder.greaterThan(date1, date2MinusOneWeek));

Ugly, but at least it works.
If anyone has any better ideas I'd still love to hear them!


Top
 Profile  
 
 Post subject: Re: JPA Criteria date/time operators
PostPosted: Thu Jan 12, 2012 7:24 pm 
Newbie

Joined: Fri Jul 30, 2010 1:17 pm
Posts: 2
I have been trying to solve a nearly identical problem. I need to check if the days between two dates in the database is less than N days. I could do something similar and store the number of days between in the database, but I would much rather handle it in the query. I don't suppose you have had any luck find a more elegant solution?


Top
 Profile  
 
 Post subject: Re: JPA Criteria date/time operators
PostPosted: Thu Jan 12, 2012 7:31 pm 
Newbie

Joined: Fri Jul 22, 2011 5:04 pm
Posts: 8
I never did find a solution, sorry.


Top
 Profile  
 
 Post subject: Re: JPA Criteria date/time operators
PostPosted: Fri Jan 13, 2012 11:10 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 10:24 am
Posts: 25
Since it varies by database implementation, you might end up having to use the "function" method of CriteriaBuilder:
Code:
builder.greaterThan(builder.get("date1"),builder.function("subdate", //For mysql
                           Date.class,
                           root.get("date2"), builder.literal(7)))


I'm not sure how it would work with the regular date_sub function since that takes a different syntax than this method provides for. You could use subtime alternately with a string format to subtract anything else you wanted off. I haven't seen any methods built-in to JPA to do date/time functions, possibly due to databases being too different on it. It's probably possible to make them using classes extending org.hibernate.ejb.criteria.expression.function.ParameterizedFunctionExpression<X> although they end up being database specific.


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.