java 我不能在我的代码中使用 findOne() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49224010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't use findOne() method in my code
提问by mattt
I have error in my app, because I use findOne() method. 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 :
我的应用程序有错误,因为我使用了 findOne() 方法。下面我的简单代码。在 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;
required: org.springframework.data.domain.Example
found: java.lang.String
reason: cannot infer type-variable(s) S (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example)
接口 org.springframework.data.repository.query.QueryByExampleExecutor 中的 findOne 方法不能应用于给定类型;
要求:org.springframework.data.domain.Example
found:java.lang.String
原因:无法推断类型变量 S(参数不匹配;java.lang.String 无法转换为 org.springframework.data.domain。例子)
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> {
}
回答by Jose Da Silva
Use findByIdor getOneinstead of findOnewhen 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.
您可以通过示例找到如何使用 find。
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);
回答by lucsbelt
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
此机制在查询创建文档中进行了解释
回答by Mathias Skovgaard Skovgaard
I had something like it. Its because you are using a newer version.
我有类似的东西。这是因为您使用的是较新的版本。
You can fix it by:
您可以通过以下方式修复它:
return userRepository.findById(email).orElse(null);