我不能在我的代码中使用findOne()方法

mat*_*ttt 6 java spring hibernate spring-data-jpa spring-boot

我的应用程序中有错误,因为我使用了findOne()方法.在我的简单代码下面.在User类中,我的id是String email,而我正在尝试在我的类UserService中使用id,如下所示:

public User findUser(String email){
    return userRepository.findOne(email);
}
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

接口org.springframework.data.repository.query.QueryByExampleExecutor中的方法findOne不能应用于给定的类型;
required:org.springframework.data.domain.Example
found:java.lang.String
reason:无法推断type-variable(s)S(参数不匹配; java.lang.String无法转换为org.springframework.data.domain.例)

用户类:

@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;
}
Run Code Online (Sandbox Code Playgroud)

和UserRepository:

public interface UserRepository extends JpaRepository<User, String> {
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*lva 13

如果只想按id搜索,请使用findByIdgetOne而不是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
    }
}
Run Code Online (Sandbox Code Playgroud)

函数findOne()接收a 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); 
Run Code Online (Sandbox Code Playgroud)


小智 7

我有类似的东西。这是因为您使用的是较新的版本。

您可以通过以下方式修复它:

return userRepository.findById(email).orElse(null);
Run Code Online (Sandbox Code Playgroud)


小智 6

JpaRepository 中的 findOne 方法定义为:

<S extends T> Optional<S> findOne(Example<S> example)
Run Code Online (Sandbox Code Playgroud)

参考

并且你正在传递一个字符串作为参数。如果要通过 User.email 查找,则必须将方法定义为:

User findOneByEmail (String email);
Run Code Online (Sandbox Code Playgroud)

此机制在查询创建文档中进行了解释