我在我的应用程序的错误,因为我用findOne()方法。下面我简单的代码。在User类我的ID是字符串的电子邮件,这就是ID我想在我的课UserService这样的使用方法:
public User findUser(String email){
return userRepository.findOne(email);
}
但我有此错误:
方法findOne接口org.springframework.data.repository.query.QueryByExampleExecutor不能被应用到给定的类型; 要求:org.springframework.data.domain.Example 发现:java.lang.String中 原因:不能推断类型变量(S)S(自变量不匹配; java.lang.String中不能转换到org.springframework.data.domain.Example)
用户等级:
@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;
}
和UserRepository:
public interface UserRepository extends JpaRepository<User, String> {
}
使用findById或当你想仅仅通过ID搜索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
}
}
该功能findOne()
收到Example<S>
,这种方法是使用例如被发现,所以你需要提供的示例对象和领域进行检查。
你可以找到如何例子使用find。
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.matchers
但它基本上是类似的东西。
User user = new User();
person.setName("Dave");
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("name")
.withIncludeNullValues()
.withStringMatcherEnding();
Example<User> example = Example.of(user, matcher);
在JpaRepository方法findOne被定义为:
<S extends T> Optional<S> findOne(Example<S> example)
和哟传递一个String作为参数。如果你想通过User.email找到方法,已被定义为:
User findOneByEmail (String email);
该机制在qazxsw POI解释