java - 如何使用 JPA 搜索字段中的子字符串?

标签 java jpa

我的应用程序使用 JPA 访问后端数据库。

我有一个映射到表的 Java 类。该类有一个由一系列“0”和“1”组成的字符串字段(称为状态)。我需要根据字段的第二个字符选择一些记录。这是我可以在不使用 JPA 的情况下执行的操作(我使用的是 MS SQLServer)。

SELECT * FROM Machines where SUBSTRING(status, 2, 1) = '1'

我如何使用 JPA 完成它?

最佳答案

JPA中有一个SUBSTRING函数:

http://download.oracle.com/otn-pub/jcp/persistence-2.0-fr-eval-oth-JSpec/persistence-2_0-final-spec.pdf

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/

相关文章:

java - 构建条件 API 查询以避免 MultipleBagFetchException

java - 使用 JNDI 连接到 AD - 为什么必须以用户名@域的格式提供安全主体?

java - Spring liquibase自定义ImplicitNamingStrategy

java - Netbeans 当前项目文件路径

Java:解密位置未知的凯撒密码

java - JPA 删除所有不适用于 Google 应用引擎的内容

java - 标准 API JPA

database - JPA 缓存与数据库的并发访问保持同步

Java/ Gradle : How can I access gradle project properties from Java code

java - 如何设计一个具有许多独立流程的企业应用程序?