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);
}
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)
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> {
}
The method findOne in JpaRepository are defined as:
Reference
and yo are passing a String as parameter. If you want to find by User.email the method has to be defined as:
This mecanism is explained in query creation document
I had something like it. Its because you are using a newer version.
You can fix it by:
Use findById or getOne instead of findOne when you want to search only by id.
The function
findOne()
receives aExample<S>
, this method is used to find by example, so you need to provide the example object and the fields to check.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
But it is basically something like.