如何使用JPA在字段上搜索子字符串? [英] How can I use JPA to search for a substring on a field?

查看:105
本文介绍了如何使用JPA在字段上搜索子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application uses JPA to access the backend database.

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

I have a Java class mapped to a table. The class has a string field (called status) that consists of a series of "0"s and "1"s. I need to select a few records based on the field's second character. Here is what I can do without using JPA (I am using MS SQLServer).

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

如何使用JPA进行操作?

How can I do it using JPA?

推荐答案

JPA中有一个SUBSTRING函数:

There is a SUBSTRING function in JPA:

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])

SUBSTRING(string_primary, simple_arithmetic_expression [, simple_arithmetic_expression])

(...)

第二和第三SUBSTRING函数的参数表示要返回的子字符串的起始位置和
长度。这些参数是整数。
第三个参数是可选的。如果未指定,则返回从字符串的起始位置到结尾的子字符串
。字符串的
第一个位置用1表示.SUBSTRING函数
返回一个字符串。

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.

这篇关于如何使用JPA在字段上搜索子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆