我不能在我的代码中使用findOne()方法 [英] I can't use findOne() method in my code

查看:1505
本文介绍了我不能在我的代码中使用findOne()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

public User findUser(String email){
    return userRepository.findOne(email);
}

但我有这个错误:


方法findOne不能应用于给定类型;

required:org.springframework.data.domain .Example

found:java.lang.String

reason:无法推断type-variable(s)S
(参数不匹配; java.lang.String无法转换to org.springframework.data.domain.Example)

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)

用户类:

@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:

and UserRepository:

public interface UserRepository extends JpaRepository<User, String> {
}


推荐答案

使用 findById getOne 而不是 findOne ,只想按ID搜索。

Use findById or getOne instead of findOne when you want to search only by id.

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()收到示例< S> ,此方法用于查找示例,因此您需要提供示例对象和要检查的字段。

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.

您可以通过示例找到如何使用find。

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

但它基本上就像是。

User user = new User();                          
person.setName("Dave");                           

ExampleMatcher matcher = ExampleMatcher.matching()     
    .withIgnorePaths("name")                         
    .withIncludeNullValues()                             
    .withStringMatcherEnding();

Example<User> example = Example.of(user, matcher); 

这篇关于我不能在我的代码中使用findOne()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆