JPA规范中的“like”操作失败。

huangapple go评论131阅读模式
英文:

JPA Specification like fails

问题

以下是翻译好的内容:

我有以下代码来搜索consedoc字段,但我看到它并不搜索,而是告诉我记录的数量。

public static Specification<Solicitud> likeConsedoc(String consedoc) {
    if (consedoc.isEmpty() || consedoc.isBlank() || consedoc == null) {
        System.out.println("Consedoc为空或空白");
        return (root, query, criteriaBuilder) -> criteriaBuilder.isTrue(criteriaBuilder.literal(true));
    }
    return (root, query, criteriaBuilder) ->
        criteriaBuilder.like(root.get(Solicitud_.CONSEDOC), "%" + consedoc + "%");
}

在日志中,我可以看到正在执行查询,我可以看到计数子句,这不是我想要的。

select count(solicitud0_.id) as col_0_0_ from patrimonio.solicitud_usuario solicitud0_ where solicitud0_.consedoc like ?

我使用Postman进行了测试,但没有返回任何记录。

有人能帮我吗,或者建议我在前端使用什么来过滤数据。我正在使用Angular,在后端使用Spring Boot进行分页和筛选。

事实上,我之前没有注意到这一点。有了这个,我看到后端工作得很好。但是在前端,如果出现故障,它不会显示我搜索到的记录,只会显示总数。

英文:

I have the following code to do the search for the consedoc field, but I see that it does not search but rather tells me the number of records

public static Specification&lt;Solicitud&gt;likeConsedoc(String consedoc){
		if(consedoc.isEmpty() || consedoc.isBlank() || consedoc==null) { //carga todos los datos inicialmente sin filtrar
			System.out.println(&quot;Consedoc null or empty&quot;);
			return (root, query, criteriaBuilder)-&gt; criteriaBuilder.isTrue(criteriaBuilder.literal(true));
		}
		return (root, query, criteriaBuilder)-&gt;
		criteriaBuilder.like(root.get(Solicitud_.CONSEDOC), &quot;%&quot;+consedoc+&quot;%&quot;);
	}

In the logs I can see the query being executed and I can see the count clause which is not what I want.

select count(solicitud0_.id) as col_0_0_ from patrimonio.solicitud_usuario solicitud0_ where solicitud0_.consedoc like ?

I did the test with postman and it does not return any record
enter image description here

Can someone help me, or suggest me to use to filter the data from my front end. I am using Angular and I am doing the pagination and filtering in the backend with Spring Boot

Truth is, I hadn't noticed that. With this I see that the backend is working fine. But in the frontend if I have a failure it does not show me the searched records it only shows me the total
enter image description here

答案1

得分: 0

Spring分页从第0页开始。通过传入pageNumber=1,pageSize=15,你告诉Spring跳过前15个结果 - 因为此查询只有1个结果,所以显示0个结果。这些信息包含在返回的结果中,其中告诉你总共有1个totalElements,总共有1页,之后没有更多记录了。

英文:

Spring Pagination starts with page 0. By passing in pageNumber=1, pageSize=15 you are telling Spring to skip the first 15 results - since there is only 1 result for this query, you get 0 results displayed. This information is contained in the returning results where it tells you there was 1 totalElements, 1 page in total, and there are no more records after this one.

huangapple
  • 本文由 发表于 2023年8月10日 22:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定