26

I want to have a JPQL query that may look like:

entityManager.createQuery("Select a.* from A a WHERE CAST(a.num AS TEXT) LIKE '%345%' ", A.class);

where a.num is an integer. I want to cast this to String to use the LIKE criteria. However above casting doesnt work in JPQL. Any idea about how can I implement this?

3
  • 1
    You may need to use a native query to do this kind of cast. Commented Sep 13, 2017 at 4:45
  • except that "SELECT *" doesnt exist in JPQL. Who knows what is "doesnt work" ... maybe some SQL is invoked? Commented Sep 13, 2017 at 15:09
  • @billyfrost edited the query to include a.*. I meant the casting doesnt work like this in jpql. The exception goes like... cannot CAST to TEXT. I am using JPA entitymanager to create the query. Commented Sep 13, 2017 at 17:18

7 Answers 7

19

From hibernate document, "cast(... as ...)", where the second argument is the name of a Hibernate type. According list of Hibernate Types it should be string (case sensitive!).

So request should be:

entityManager.createQuery("select a.* from A a WHERE CAST(a.num AS string) LIKE '%345%' ", A.class);

Was taken and checked from Caused by: org.hibernate.QueryException: Could not resolve requested type for CAST : INT answer.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a ton for the clue on string's case sensitivity; String wasn't working..
18

Could you be more specific, what your problem is? Do you have some kind of error or it the result of the query is just wrong?

Because this code works fine for me:

session.createQuery("from MyObject where CAST(id as text) like :id").setParameter("id", "%1").getResultList();

I am using Hibernate 5.2 and Postgresql 9.5. Also, if you are having trouble understanding what goes wrong, you could try editing hibernate.cfg.xml or whatever configuration file you are using to make Hibernate show queries it sends, by doing something like this:

<property name="hibernate.show_sql">true</property>

4 Comments

Yes, this code will work as you are using hibernate's session to create the query. Whereas I am using JPA entitymanager to do the same. I had tried the same casting in my query too but failed with an exception, something like "Cannot CAST to text" (dont remember exactly as I dont have the code with me right now). Seems JPQL doesnt support castings.
Well, the the only other option, that I know, is using native queries, as was adviced before. Though it usually seems a bit ugly (maybe, even seroiusly ugly), for some operations it is the only way, as far as I know.
Argh it's case sensitive for casting types? TEXT produced an error but text works just fine.
It should be CAST(id as string) like @grigory-kislin said
13

I also have the same need. This should work with JPA 2.0 and Hibernate 5.0.2:

entityManager.createQuery(
    "Select a.* from A a WHERE CONCAT(a.num, '') LIKE '%345%' ", A.class);

Comments

5

You can simply use CAST(num as string). It worked for me

Comments

1

Well you can use to_char() function in the select clause but, you will need to select all the a.num field separately and not with *.

And in postgresql you will need to specify a mask for to_char()function, so it would be to_char(field, mask), for example we can supply 'FM999999999999999999' as a mask to accept the maximum possible digits.

Your query would be something like this:

Select *, to_char(a.num, 'FM999999999999999999') as num from A a WHERE num LIKE '%345%'

You can take a look at Postgresql Data Type Formatting Functions for further details.

To write the query in your code with EntityManager you can create a native query using .createNativeQuery() method, this is how should be your code:

em.createNativeQuery("Select *, to_char(a.num, 'FM999999999999999999') as num from A a WHERE num LIKE '%345%'");

2 Comments

The issue is more related to JPQL than to postgres. I am using JPA's entitymanager.createQuery(). I will have to check whether this function works as a part of JPQL query.
@Pratap Yes with EntityManager you can create a native query like this èm.createNativeQuery()`, take a look at my edit.
1

The correct query is :

entityManager.createQuery("Select a.* from A a WHERE CAST(a.num AS String) LIKE '%345%' ", A.class);

1 Comment

That would give: org.hibernate.QueryException: Could not resolve requested type for CAST : String
0

You can use native SQL functions in JQPL code to perform this CAST.

For example converting String to Number in Oracle DB can look like this:

SELECT t 
FROM TestEntity t 
WHERE t.numeric_column = (SELECT FUNCTION('to_number', t2.string_column) FROM TestEntity2 t2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.