Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use findOne() in my code with JpaRepository [duplicate]

I have a project with class Personne

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Personne() {
        super();
    }
}

and the interface of PersonRepository is here

package com.example.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.entities.Personne;

public interface IPersonneRepository extends JpaRepository<Personne, Long> {    

}

And in the Controller i add the methode of CRUD (Create, Read, Update, and Delete) those methodes are in this class

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;
        } 

    }
}

the application.properties is

spring.datasource.url = jdbc:mysql://localhost:3306/spring_boot_DB? 
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.show-sql = true 
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming- 
strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MySQL5Dialect
server.port=8081

I tried to change the JpaRepository in CrudRepository but the same problem the same mistake is The method findOne(Example) in the type QueryByExampleExecutor is not applicable for the arguments (Long) i tried to change the method to getOne() but is not perfect I have already used this method before but this time it does not work I do not know why in the same editor. I know very well that I did not yet have the techniques to solve this kind of problem because of the experience that I ask you to show me or due the problem and thank you in advance for the help :)

like image 638
BMW83 Avatar asked Mar 29 '18 16:03

BMW83


2 Answers

Try using the following 2 options based on the requirement :-

yourRespository.findById(id).get();// it will return an Optional instead of null

yourRespository.findById(id).orElse(null); // this will return null if you have proper null checks

There has been some changes with the recent versions of spring data jpa and they have removed the findOne() method which used to work earlier. you can check the post here for reference - https://stackoverflow.com/a/44103020/2600196

Or revert back to old spring data jpa version

like image 103
kakabali Avatar answered Sep 22 '22 01:09

kakabali


Use findById() instead of findOne().

like image 27
Christopher Avatar answered Sep 21 '22 01:09

Christopher