我的应用程序使用 JPA 访问后端数据库。
我有一个映射到表的 Java 类。该类有一个由一系列“0”和“1”组成的字符串字段(称为状态)。我需要根据字段的第二个字符选择一些记录。这是我可以在不使用 JPA 的情况下执行的操作(我使用的是 MS SQLServer)。
SELECT * FROM Machines where SUBSTRING(status, 2, 1) = '1'
我如何使用 JPA 完成它?
最佳答案
JPA中有一个SUBSTRING函数:
4.6.17.2.1“字符串函数”
(...)
SUBSTRING(string_primary, simple_arithmetic_expression [, simple_arithmetic_expression])
(...)
The second and third arguments of the SUBSTRING function denote the starting position and length of the substring to be returned. These arguments are integers. The third argument is optional. If it is not specified, the substring from the start position to the end of the string is returned. The first position of a string is denoted by 1. The SUBSTRING function returns a string.
关于java - 如何使用 JPA 搜索字段中的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8115217/