有没有办法订购我的JPQL查询尝试的WHERE条件?

时间:2016-06-14 14:18:31

标签: java sql hibernate jpa jpql

我有一个查询

SELECT CAST(p.propertyId AS long) FROM Property p
 WHERE p.propertyId IS NOT NULL
   AND p.propertyId NOT LIKE '%-%'
   AND p.propertyId NOT LIKE '%+%'

这很好用,因为WHERE条件成功地过滤掉了propertyId的所有非数字值。但是如果我尝试使用强制转换作为条件

SELECT p FROM Property p
 WHERE p.propertyId IS NOT NULL
   AND p.propertyId NOT LIKE '%-%'
   AND p.propertyId NOT LIKE '%+%'
   AND CAST(p.propertyId AS long) BETWEEN 999999 AND 10000000

它失败并显示错误java.sql.SQLException: ORA-01722: invalid number

有没有办法强制最后一个条件只检查前三个传球?

1 个答案:

答案 0 :(得分:1)

如果您的第一个请求过滤掉所有非数字值,您可以从结果中选择:

SELECT p FROM
(
SELECT p1 FROM Property p1
 WHERE p1.propertyId IS NOT NULL
   AND p1.propertyId NOT LIKE '%-%'
   AND p1.propertyId NOT LIKE '%+%'
)
where CAST(p.propertyId AS long) BETWEEN 999999 AND 10000000