简体   繁体   English

Spring JpaRepository findAll(example) 方法返回一个空列表

[英]Spring JpaRepository findAll(example) method is returning an empty list

I am working on an application in Spring and have run into an issue I can't find any info on.我正在开发 Spring 中的应用程序,遇到了一个我找不到任何信息的问题。

I have a SpringMVC Get route that I expect to return a list of Users filtered based on the user's request params.我有一个 SpringMVC Get 路由,我希望返回基于用户请求参数过滤的用户列表。

Here's the specific code:下面是具体代码:

    @GetMapping("/admin/users/filtered")
    public String adminUserPageFiltered(@RequestParam String role, Model model) {
        ExampleMatcher matcher = ExampleMatcher
                .matchingAll()
                .withIgnoreNullValues()
                .withMatcher("role", contains().ignoreCase());
        User example = new User();
        example.setRole(role);
        System.out.println("example role: " + example.getRole());
        Iterable<User> userList = userRepository.findAll(Example.of(example, matcher));
        System.out.println("Userlist: " + userList);
        model.addAttribute("users", userList);
        return "adminUserPage";

    }

As you can see here, the basic program flow is that a param is passed to this route, it creates a new User object with the values set by the param (currently only "Role" but I expect that to change over time)正如您在此处看到的,基本程序流程是将参数传递给此路由,它使用参数设置的值创建一个新用户 object(目前只有“角色”,但我希望随着时间的推移而改变)

The findAll method then looks for all records where role = the role param value;然后 findAll 方法查找其中角色 = 角色参数值的所有记录;

In other words, my SQL query would look like this:换句话说,我的 SQL 查询将如下所示:

SELECT * FROM users WHERE role="myParam"

Running this query where role="admin" returns 1 record currently, which is expected.运行此查询 where role="admin" 当前返回 1 条记录,这是预期的。

But the findAll(example) in the java code above returns 0 records.但是上面 java 代码中的 findAll(example) 返回 0 条记录。

Can anyone tell me why the findAll method is not finding any records, and returning and empty list?谁能告诉我为什么 findAll 方法没有找到任何记录,并返回和清空列表?

Here is some other code that may be relevant这是其他一些可能相关的代码

User.java用户.java

public class User {

    @Id
    @Column(name = "user_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String username;
    private String password;
    private String role;
    private boolean enabled;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public String getRole() {
        return role;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public Long getId() {
        return id;
    }

}

UserRepository.java UserRepository.java

import com.example.sprintauthexample.model.User;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;


public interface UserRepository extends JpaRepository<User, Long> {

    @Query("SELECT u FROM User u WHERE u.username = :username")
    public User getUserByUsername(@Param("username") String username);

    @Query("SELECT COUNT(u) FROM User u WHERE u.enabled = true")
    public int getUserCount();

    @Query("SELECT COUNT(role) FROM User u WHERE u.role='USER' AND u.enabled=true")
    public int getUserRoleCount();
}

EDIT Following advice from Jens Schrauder, I logged the generated query, which is below:编辑根据 Jens Schrauder 的建议,我记录了生成的查询,如下所示:

select user0_.user_id as user_id1_0_, user0_.enabled as enabled2_0_, user0_.password as password3_0_, user0_.role as role4_0_, user0_.username as username5_0_ from users user0_ where (lower(user0_.role) like ? escape ?) and user0_.enabled=?

with binding带装订

2021-02-17 20:29:35.214 TRACE 14700 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%admin%]
2021-02-17 20:29:35.214 TRACE 14700 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [CHAR] - [\]
2021-02-17 20:29:35.215 TRACE 14700 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BOOLEAN] - [false]

To be completely honest, I'm not completely sure what this means, exactly.老实说,我不完全确定这到底意味着什么。 I'm reading up on it currently, but if anyone knows, i'm certainly grateful if you share your knowledge or can point me in the right direction.我目前正在阅读它,但如果有人知道,如果你分享你的知识或能指出我正确的方向,我当然会感激不尽。

I'm happy to add more info as needed.我很乐意根据需要添加更多信息。 Thanks to anyone who finds the answer!感谢任何找到答案的人!

I'm no Java expert but I've fixed the issue, here's how.我不是 Java 专家,但我已经解决了这个问题,方法如下。

The "enabled" field is set to TRUE for every user in the db.对于数据库中的每个用户,“启用”字段都设置为 TRUE。 The example was generated enabled to "false"生成的示例启用为“假”

I believe the reason for this is that a NULL value in a boolean is false.我相信这是因为 boolean 中的 NULL 值是错误的。 That's how it would be in JavaScript, which is my main language.这就是我的主要语言 JavaScript 中的情况。 Java I'm still learning, but I'm happy with this explanation. Java 我还在学习,但我对这个解释很满意。 Perhaps someone with more understanding and experience with Java can correct me.也许对 Java 有更多了解和经验的人可以纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring CrudRepository方法findAll()返回一个空列表 - Spring CrudRepository method findAll() returns an empty list 使用 Spring Data Rest 从自定义 JpaRepository 方法返回投影列表 - Returning a list of projection from a custom JpaRepository method with Spring Data Rest Spring CrudRepository findAll() 返回空 - Spring CrudRepository findAll() returning empty JpaRepository findAll() 返回空结果 - JpaRepository findAll() returns empty result JpaRepository .findAll 方法通过邮递员返回空值和重复列表 - JpaRepository .findAll method returns null values and reapeating list via postman JPA Spring 存储库 findAll() 返回一个空列表 - JPA Spring repository findAll() returns an empty List 在 JpaRepository findAll() 方法中返回对象的浅拷贝 - Return a shallow copy of an object in JpaRepository findAll() method 在 ModelAndView 中使用 Jparepository 的 FindAll 方法 - Using Jparepository's FindAll method with ModelAndView Spring Data JpaRepository findAll(Iterable <ID> ids)+ findAll(排序排序) - Spring Data JpaRepository findAll(Iterable<ID> ids) + findAll(Sort sort) <SpringBoot / Hibernate>调用 JpaRepository.findAll 时的 InvocationException(示例) - <SpringBoot / Hibernate> InvocationException on calling JpaRepository.findAll(Example example)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM