Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Equivalent of ROUND in criteria query in JPA 2
Equivalent of ROUND in criteria query in JPA 2 [message #548762] Thu, 22 July 2010 22:05 Go to next message
Chad Retz is currently offline Chad RetzFriend
Messages: 6
Registered: July 2010
Junior Member
Is there a criteria equivalent to the commonly used ROUND (or TRUNC in some vendors) function in SQL? I am unable to locate it.
Re: Equivalent of ROUND in criteria query in JPA 2 [message #548983 is a reply to message #548762] Fri, 23 July 2010 15:27 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Criteria api implemented what was already included in JPQL, and there is no ROUND or TRUNC available in JPQL so it cannot be used directliy. Criteria expressions do allow using the function(String name, Class<T> returntype, Expression<?>... args) though to specify ROUND or any other database call that is not directly supported. Something like cb.function("ROUND", Integer.class, expression) might work.

Unfortunately, if you port to a database that uses TRUNC instead, your query will fail.

EclipseLink's expression framework does have support for ROUND in the org.eclipse.persistence.expressions.ExpressionMath class.

Best Regards,
Chris
Re: Equivalent of ROUND in criteria query in JPA 2 [message #550906 is a reply to message #548762] Wed, 04 August 2010 15:30 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

There is a bug logged for adding additional function support.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=321725

If you need database independent support for ROUND please add ROUND to this bug and vote for it.


James : Wiki : Book : Blog : Twitter
Re: Equivalent of ROUND in criteria query in JPA 2 [message #550987 is a reply to message #550906] Wed, 04 August 2010 18:54 Go to previous message
Chad Retz is currently offline Chad RetzFriend
Messages: 6
Registered: July 2010
Junior Member
Well, I figure it is already somewhat there with ExpressionOperator#round(), so I figure something along the following lines would work for JPA:

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T extends Number> Expression<T> round(EntityManager em, 
        Expression<T> value, int length) {
    return new FunctionExpressionImpl(em.getMetamodel(), ClassConstants.NUMBER,
            ExpressionOperator.round().expressionFor(((InternalSelection) value).
                    getCurrentNode(), length), Arrays.asList(value), "round");
}


...but I haven't tested it.
Previous Topic:setMaxResults() doesn't work with MySql - sql syntax error
Next Topic:Lazy fetched ManyToOne field of an embedded object of a newly persisted entity resets to null
Goto Forum:
  


Current Time: Thu Mar 28 19:46:19 GMT 2024

Powered by FUDForum. Page generated in 0.03777 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top