3

我正在从 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输入字符串的第一个字母是否与N字母长的 location_key 匹配。那就是输入字符串Canada,ON应该与类似的东西匹配:CanadaCanC

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!

4

1 回答 1

1

我不确定你的查询是如何工作的,但你不应该使用 CriteriaBuilder.substring(Expression, int) 而不是 LEFT() 吗?如果您提供您希望生成的实际 SQL,我可能会提出更好的想法。

于 2011-04-27T06:44:16.430 回答