Can't use findOne() dans mon projet Spring boot
Salut mes amis
Je suis entrain de réalisé un projet de spring j'ai une classe de Personne
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @SuppressWarnings("serial")
@Entity
public class Personne implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(length = 80, name = "NOM")
private String nom;
@Column(length = 80, name = "PRENOM")
private String prenom;
@Column(length = 80, name = "ADRESSE")
private String adresse;
@Column(length = 80, name = "EMAIL")
private String email;
@Column(length = 80, name = "TELEPHONE")
private String telephone;
//avec les getters et le setters et le constructeurs sans et avec parametres |
et la l'interface IPersonneRepository
Code:
1 2 3 4 5
| import com.example.entities.Personne;
public interface IPersonneRepository extends JpaRepository<Personne, Long> {
} |
Et le controlleur de personne sous le nom de PersonneController qui définit les methode d'ajout, Modification, Suppression avec l'affichage de toute les personnes
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.entities.Personne;
import com.example.repositories.IPersonneRepository;
@RestController
@RequestMapping(value = "/personne")
public class PersonController {
@Autowired
private IPersonneRepository iPersonneRepository;
@PostMapping(value = "/create")
public Personne createPersonne(@RequestBody Personne personne) {
return iPersonneRepository.save(personne);
}
@PutMapping(value = "/update/{id}")
public Personne updatePersonne(@PathParam(value = "id") Long id, @RequestBody Personne personne) {
if (id != null) {
Personne p = iPersonneRepository.findOne(id);
if (p != null) {
iPersonneRepository.save(personne);
}
}
return personne;
}
@DeleteMapping(value = "/delete/{id}")
public void delete(@PathParam(value = "id") Long id) {
if (id != null) {
Personne p = iPersonneRepository.findOne(id);
if (p != null) {
iPersonneRepository.delete(p);
}
}
}
@GetMapping(value = "/all")
public List<Personne> allPerson() {
return iPersonneRepository.findAll();
}
@GetMapping(value = "/per/{id}")
public Personne getOne(@PathParam(value = "id") Long id) {
if (id != null) {
Personne p = iPersonneRepository.findOne(id);
return p;
}
}
} |
je ne sais pas où du le problème? Pourquoi m'afficher ce message d'erreur sur tout les commandes de findOne "The method findOne(Example<S>) in the type QueryByExampleExecutor<Personne> is not applicable for the arguments (Long)" Malgré que j'ai déjà utilisé le getOne() au lieu de findOne() qui ne me donne pas la même requête pour me recevoir une personne.
Aider moi SVP
La version de spring qui empêche findOne()
Salut mes amis
Après avoir une période j'ai trouvé la solution qui due exactement de la version de Spring qui nous utilise dans notre projet. Pour cela les versions spring de série 1.4.5 et quelque autre de suite sont des version qu'il peut utiliser findOne() sans aucun soucie par contre les nouveaux versions n'accepte pas. Et pour cela je devrai utiliser findByid(id).orElse(null);
Et pour cela notre code sera comme suit
Code:
Personne p = iPersonneRepository.findByid(id).orElse(null);
et le problème résolu