Spring JpaRepository findAll(example) 方法返回一个空列表
Spring JpaRepository findAll(example) method is returning an empty list
我正在 Spring 开发一个应用程序,运行 遇到一个问题,我找不到任何相关信息。
我有一个 SpringMVC Get 路由,我希望它 return 根据用户的请求参数过滤的用户列表。
具体代码如下:
@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";
}
正如你在这里看到的,基本的程序流程是将一个参数传递给这个路由,它创建一个新的用户对象,其值由参数设置(目前只有“角色”,但我希望它会改变随着时间的推移)
然后 findAll 方法查找角色 = 角色参数值的所有记录;
换句话说,我的 SQL 查询将如下所示:
SELECT * FROM users WHERE role="myParam"
运行 此查询 where role="admin" return当前有 1 条记录,这是预期的。
但是上面java代码中的findAll(example)returns 0条记录。
谁能告诉我为什么 findAll 方法找不到任何记录,并且 returning 和空列表?
这是一些可能相关的其他代码
User.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
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();
}
编辑
根据 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=?
绑定
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]
老实说,我不太确定这到底是什么意思。我目前正在阅读它,但如果有人知道,如果您能分享您的知识或能为我指明正确的方向,我将不胜感激。
我很乐意根据需要添加更多信息。感谢任何找到答案的人!
我不是 Java 专家,但我已经解决了这个问题,方法如下。
数据库中的每个用户的“启用”字段都设置为 TRUE。该示例生成为启用“false”
我认为这是因为布尔值中的 NULL 值是假的。在我的主要语言 JavaScript 中就是这样。 Java 我还在学习,但我对这个解释很满意。也许对 Java 有更多了解和经验的人可以纠正我。
我正在 Spring 开发一个应用程序,运行 遇到一个问题,我找不到任何相关信息。
我有一个 SpringMVC Get 路由,我希望它 return 根据用户的请求参数过滤的用户列表。
具体代码如下:
@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";
}
正如你在这里看到的,基本的程序流程是将一个参数传递给这个路由,它创建一个新的用户对象,其值由参数设置(目前只有“角色”,但我希望它会改变随着时间的推移)
然后 findAll 方法查找角色 = 角色参数值的所有记录;
换句话说,我的 SQL 查询将如下所示:
SELECT * FROM users WHERE role="myParam"
运行 此查询 where role="admin" return当前有 1 条记录,这是预期的。
但是上面java代码中的findAll(example)returns 0条记录。
谁能告诉我为什么 findAll 方法找不到任何记录,并且 returning 和空列表?
这是一些可能相关的其他代码
User.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
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();
}
编辑 根据 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=?
绑定
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]
老实说,我不太确定这到底是什么意思。我目前正在阅读它,但如果有人知道,如果您能分享您的知识或能为我指明正确的方向,我将不胜感激。
我很乐意根据需要添加更多信息。感谢任何找到答案的人!
我不是 Java 专家,但我已经解决了这个问题,方法如下。
数据库中的每个用户的“启用”字段都设置为 TRUE。该示例生成为启用“false”
我认为这是因为布尔值中的 NULL 值是假的。在我的主要语言 JavaScript 中就是这样。 Java 我还在学习,但我对这个解释很满意。也许对 Java 有更多了解和经验的人可以纠正我。