Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何在criteria builder中使用子字符串_Postgresql_Jpa_Spring Data Jpa_Substring_Criteria - Fatal编程技术网

Postgresql 如何在criteria builder中使用子字符串

Postgresql 如何在criteria builder中使用子字符串,postgresql,jpa,spring-data-jpa,substring,criteria,Postgresql,Jpa,Spring Data Jpa,Substring,Criteria,我想将字符串11.0.3.200转换为0011.0000.0003.0200,以便对该字符串进行降序排序 Expression<String> majorVer = cb.function("SUBSTRING_INDEX", String.class, uf.get(sortAttr), cb.literal("."), cb.literal(1)); //11 Expression<String> lpadMajorValue = cb.function(

我想将字符串
11.0.3.200
转换为
0011.0000.0003.0200
,以便对该字符串进行降序排序

Expression<String> majorVer = cb.function("SUBSTRING_INDEX", String.class, uf.get(sortAttr), cb.literal("."), cb.literal(1)); //11      
Expression<String> lpadMajorValue = cb.function("lpad", String.class, majorVer, cb.literal(4), cb.literal("0"));

Expression<String> minorVerBase = cb.function("REPLACE", String.class, uf.get(sortAttr), cb.concat(majorVer, cb.literal(".")), cb.literal(""));//0.3.200      
Expression<String> minorVer = cb.function("SUBSTRING_INDEX", String.class, minorVerBase, cb.literal("."), cb.literal(1)); //0
 Expression<String> lpadMinorValue = cb.function("lpad", String.class, minorVer, cb.literal(4), cb.literal("0"));

Expression<String> buildVerBase = cb.function("REPLACE", String.class, minorVerBase, cb.concat(minorVer, cb.literal(".")), cb.literal("")); //3.200
Expression<String> buildVer = cb.function("SUBSTRING_INDEX", String.class, buildVerBase, cb.literal("."), cb.literal(1)); //3
Expression<String> lpadBuildValue = cb.function("lpad", String.class, buildVer, cb.literal(4), cb.literal("0"));

Expression<String> revVer = cb.function("REPLACE", String.class, buildVerBase, cb.concat(buildVer, cb.literal(".")), cb.literal("")); //200
Expression<String> lpadRevisionValue = cb.function("lpad", String.class, revVer, cb.literal(4), cb.literal("0"));

Expression<String> lpadValue = cb.concat(lpadMajorValue, cb.concat(lpadMinorValue, cb.concat(lpadBuildValue, lpadRevisionValue)));

 orderList.add(cb.desc(lpadValue));

如果我在
SUBSTRING\u INDEX
中出错,请使用标准构建。如果criteria builder不支持
子字符串索引
,有什么替代方案?

JPA 2.2规范第4.6.17.2节列出了JPA中可用的功能,但不包括
子字符串索引
。我想可以安全地假设早期版本也不包含它。因此,JPA将无法将其转换为特定于数据库的内容

它也不是Postgres SQL方言的一部分。因此,正如您所注意到的,简单的传递也不起作用

幸运的是,JPA知道
LOCATE
,这听起来像是您正在寻找的

根据规范:

LOCATE函数返回给定字符串在字符串中的位置,从指定位置开始搜索。它返回字符串作为整数找到的第一个位置。第一个参数是要定位的字符串;第二个参数是要搜索的字符串;可选的第三个参数是一个整数,表示开始搜索的字符串位置(默认情况下,是要搜索的字符串的开头)。字符串中的第一个位置由1表示。如果未找到字符串,则返回0


PostgreSQL表示“函数子字符串_索引(文本)不存在”。所以没有这样的功能。所以,不要使用它,而是使用其他东西。MySQL有这个功能,但您显然没有使用MySQL。您在中的何处找到了
子字符串\u index()
?即使函数拆分部分出现在上面给出的链接中,但它仍然给出了相同的异常并表示错误:函数拆分部分(文本)不存在,这就是您查看调用的SQL的地方!!!aka调试
nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; 
nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: ERROR: function substring_index(text) does not exist"