I was trying to do a simple select using JPA:
SELECT POW(2, 10) FROM COUNTRY
So, I give it a try:
EntityManager em = getEM();
CriteriaBuilder cb = em.getCriteriaBuilder( );
CriteriaQuery< Object > cq = cb.createQuery( );
cq.from( Country.class );
cq.multiselect( cb.function( "POW", Integer.class, cb.literal( 2 ), cb.literal( 10 ) ) );
em.createQuery( cq ).getResultList( );
And this gives me back a QueryException:
Then, I tried to cast it:
EntityManager em = getEM();
CriteriaBuilder cb = em.getCriteriaBuilder( );
CriteriaQuery< Object > cq = cb.createQuery( );
cq.from( Country.class );
cq.multiselect( cb.function( "POW", Integer.class, cb.literal( 2 ), cb.literal( 10 ) ).as( Long.class ) );
em.createQuery( cq ).getResultList( );
(I have tried to cast it as String and Long. Casting to Integer results in the same exception as above.) And then I get another QueryException:
What am I doing wrong? Is there something I can do to solve this issue on the "user" side? I noticed this error occurs with the following functions:
- CHARSET
- CONNECTION_ID
- CONCAT_WS
- CONV
- DATABASE
- POW
- POWER
- FORMAT
- REPEAT
- REPLACE
- SUBSTR
- USER
- UUID
P.S.: I tried the above query using EclipseLink and it works fine. P.S.2: POW and POWER don't work and are the same function. But in the SUBSTR and SUBSTRING pair, only the first don't work. (As reported in https://stackoverflow.com/questions/45725573/queryexception-on-hibernate-when-using-criteriabuilder-function?noredirect=1#comment78415129_45725573) |