Skip to main content



      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 18:05 Go to next message
Eclipse UserFriend
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 11:27 Go to previous messageGo to next message
Eclipse UserFriend
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 11:30 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: Equivalent of ROUND in criteria query in JPA 2 [message #550987 is a reply to message #550906] Wed, 04 August 2010 14:54 Go to previous message
Eclipse UserFriend
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: Tue Jul 22 18:21:35 EDT 2025

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

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

Back to the top