Badr MOUMOUD
Badr MOUMOUD

Reputation: 355

method findOne() of JpaReposiroy Spring Data ? findOne is not applicable for the arguments (String)

// Class CompteRepository
import org.springframework.data.jpa.repository.JpaRepository;
import org.entities.Compte;

public interface CompteRepository extends  JpaRepository<Compte, String>{}
// CLASS BanqueMetierImpl``
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service // SPring couche Metier
@Transactional
public class BanqueMetierImpl implements IBanqueMetier{
    @Autowired
    private CompteRepository compteRepository;
    @Override
    public Compte consulterCompte(String code) {
        Compte cp = compteRepository.findOne(code);
        return cp;
    }

// The method findOne show up this error The method findOne(Example) in //the type QueryByExampleExecutor is not applicable for the arguments //(String)

Upvotes: 1

Views: 4261

Answers (1)

Badr MOUMOUD
Badr MOUMOUD

Reputation: 355

I think the method findOne() is unsupported by version 1.5.1.SNAPSHOT of SPRING BOOT , so in 2.0.1.SNAPSHOT it's replaced by FindById() which is a QueryByExampleExecutor it's an Optional method (see Optional in JAVA 8) so I resolved the problem like this:

@Override public Compte consulterCompte(String code) throws NotFoundException {
    Optional<Compte> cp = compteRepository.findById(code);
    return cp.orElseThrow(
        () ->  new NotFoundException("Unable to get Account with Code = " + code)
    );  
}

Upvotes: 1

Related Questions