使用 JPA2 Criteria API 查询字符串子集
我正在从 Hibernate Criteria API 升级到 JPA2 Criteria API,并且遇到了以下问题,但我似乎无法解决该问题。
在 Hibernate Criteria 中,我使用 Restrictions.sqlRestriction 来检查特定类型的字符串子集:
criteria.add(Restrictions.sqlRestriction("{alias}.location_key = left((?), length({alias}.location_key))", searchObj.getLocationKey(), Hibernate.STRING));
这本质上是检查输入字符串的前 N 个字母是否与 location_key 匹配,即 N 。代码>字母长。也就是说,输入字符串 Canada,ON
应该与以下内容匹配:Canada
、Can
或 C
。
问题是 JPA2 不允许我们像这样编写原生 sql 代码,而且我不确定如何在没有 left
函数的情况下实现类似的功能(我认为这可能是 mysql 特定的)。
任何帮助表示赞赏!
I'm upgrading from the Hibernate Criteria API to the JPA2 Criteria API, and am running into the following problem, which I can't seem to resolve.
In the Hibernate Criteria I was using Restrictions.sqlRestriction to check for a particular kind of string subset:
criteria.add(Restrictions.sqlRestriction("{alias}.location_key = left((?), length({alias}.location_key))", searchObj.getLocationKey(), Hibernate.STRING));
This essentially checks that the first N
letters of the input string match the location_key which is N
letters long. That is the input string Canada,ON
should be matched by anything like: Canada
, or Can
or C
.
The problem is that JPA2 doesn't allow us to code native sql like that, and I'm not sure how I can achieve something similar without the left
function (which I think might be mysql specific).
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的查询到底是如何工作的,但是您不应该能够使用 CriteriaBuilder.substring(Expression, int) 而不是 LEFT() 吗?如果您提供您希望生成的实际 SQL,我可能会提出更好的想法。
I'm not sure exactly how your query works, but shouldn't you be able to use CriteriaBuilder.substring(Expression, int) instead of LEFT()? I might come with better ideas if you provide the actual SQL that you wish to generate.