Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method findOne(Example<S>) in the type QueryByExampleExecutor<Contact> is not applicable for the arguments (Long)

The find methods not working for me but in other project work fine .

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.entities.Contact;

public interface ContactRepository extends JpaRepository<Contact, Long>{

}

in my controller i call find one but give works.

@RequestMapping(value="/contact/{id}",method=RequestMethod.GET)
    public Contact getContact(@PathVariable Long id){
        return repo.findOne(id); //here give a error
    }
like image 949
M.SAM SIM Avatar asked Jan 28 '23 21:01

M.SAM SIM


1 Answers

Some CRUD Repository methods got renamed in Spring Data and

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    T findOne(ID id);

is one of them. Now you should use the

public interface CrudRepository<T, ID> extends Repository<T, ID> {
    Optional<T> findById(ID id);

For more info which methods got renamed see this blog improved-naming-for-crud-repository-methods

There is still a findOne method around but this is from

public interface QueryByExampleExecutor<T> {
    <S extends T> Optional<S> findOne(Example<S> example);

which is also an interface of the SimpleJpaRepository. So that is why you got your error, since this method awaits an Example as parameter.

like image 144
C. Weber Avatar answered Jan 31 '23 08:01

C. Weber