5

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
    }
M.SAM SIM
  • 85
  • 1
  • 5
  • 1
    Which version of Spring Boot do you use? If it is 2.0.2 you should use findById method since it got renamed. – C. Weber Jun 25 '18 at 15:32

1 Answers1

9

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.

C. Weber
  • 907
  • 5
  • 18