Casting Integer to String in JPQL

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);

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.


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>

Tags:

Jpa

Jpa 2.0

Jpql