[英]I can't use findOne() method in my code
I have error in my app, because I use findOne() method.我的应用程序有错误,因为我使用了 findOne() 方法。 Below my simple code.
在我的简单代码下面。 In User class my id is String email and that's id I'm trying to use in my class UserService like this:
在 User 类中,我的 id 是 String email ,这是我试图在我的 UserService 类中使用的 id,如下所示:
public User findUser(String email){
return userRepository.findOne(email);
}
but I have this error:但我有这个错误:
method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor cannot be applied to given types;
接口 org.springframework.data.repository.query.QueryByExampleExecutor 中的方法 findOne 不能应用于给定类型;
required: org.springframework.data.domain.Example要求:org.springframework.data.domain.Example
found: java.lang.String找到:java.lang.String
reason: cannot infer type-variable(s) S (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example)原因:无法推断类型变量 S(参数不匹配;java.lang.String 无法转换为 org.springframework.data.domain.Example)
User class:用户类:
@Entity
@Data
@Table(name = "User")
public class User {
@Id
@Email
@NotEmpty
@Column(unique = true)
private String email;
@NotEmpty
private String name;
@NotEmpty
@Size(min = 5)
private String password;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Task> tasks;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLE", joinColumns = {
@JoinColumn(name = "USER_EMAIL", referencedColumnName = "email")
}, inverseJoinColumns = {@JoinColumn(name = "ROLE_NAME", referencedColumnName = "name")})
private List<Role> roles;
}
and UserRepository:和用户存储库:
public interface UserRepository extends JpaRepository<User, String> {
}
Use findById or getOne instead of findOne when you want to search only by id.当您只想按 ID 搜索时,请使用findById或getOne而不是findOne 。
public User findUser(String email){
return userRepository.getOne(email); // throws when not found or
// eventually when accessing one of its properties
// depending on the JPA implementation
}
public User findUser(String email){
Optional<User> optUser = userRepository.findById(email); // returns java8 optional
if (optUser.isPresent()) {
return optUser.get();
} else {
// handle not found, return null or throw
}
}
The function findOne()
receives a Example<S>
, this method is used to find by example, so you need to provide the example object and the fields to check.函数
findOne()
接收一个Example<S>
,该方法用于通过示例查找,因此您需要提供示例对象和要检查的字段。
You can find how to use the find by example.您可以找到如何使用示例查找。
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.matchers https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.matchers
But it is basically something like.但它基本上是这样的。
User user = new User();
person.setName("Dave");
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("name")
.withIncludeNullValues()
.withStringMatcherEnding();
Example<User> example = Example.of(user, matcher);
I had something like it.我有类似的东西。 Its because you are using a newer version.
这是因为您使用的是较新的版本。
You can fix it by:您可以通过以下方式修复它:
return userRepository.findById(email).orElse(null);
The method findOne in JpaRepository are defined as: JpaRepository 中的 findOne 方法定义为:
<S extends T> Optional<S> findOne(Example<S> example)
and yo are passing a String as parameter.你正在传递一个字符串作为参数。 If you want to find by User.email the method has to be defined as:
如果要通过 User.email 查找,则必须将方法定义为:
User findOneByEmail (String email);
This mecanism is explained in query creation document这种机制在查询创建文档中有解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.