在JPQL中将整数转换为字符串 [英] Casting Integer to String in JPQL

查看:118
本文介绍了在JPQL中将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个看起来像以下的JPQL查询:

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

其中a.num是整数.我想将其强制转换为String以使用LIKE标准. 但是,上面的转换在JPQL中不起作用.关于如何实现这一点的任何想法吗?

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?

推荐答案

您能具体一点吗,您的问题是什么?您是否有某种错误,或者查询结果是错误的?

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

我正在使用Hibernate 5.2和Postgresql 9.5. 另外,如果您在理解问题出在哪里时,可以尝试执行以下操作来编辑hibernate.cfg.xml或用于使Hibernate显示其发送的查询的任何配置文件:

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>

这篇关于在JPQL中将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆