IphoneVore
IphoneVore

Reputation: 13

Java: I'm not able to use the crud Repository deleteByID() to remove instance from my entity

As a beginner in OOP, I'm facing a lot of problems in my exercise. I would like to know, how can I remove employe from myrunner.java with the deleteById().

My Repository

import com.ipiecoles.java.eval2x0.model.Employe;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.awt.print.Pageable;
import java.util.List;

@Repository
public interface EmployeRepository extends PagingAndSortingRepository<Employe, Long> {
    /**
     * Méthode qui cherche un employé selon son matricule
     * @param matricule
     * @return l'employé de matricule correspondant, null sinon
     */
    Employe findByMatricule(String matricule);
}

My runner

private void afficheMenuSupprimerEmploye() {
        System.out.println("=====================================");
        System.out.println("Suppression de l'employé de matricule : ");
        String MatSupprimer = litString(REGEX_MATRICULE); // regex conditions
        System.out.println(MatSupprimer);

        Employe deleteEmploye =  employeRepository.findByMatricule(MatSupprimer);
        System.out.println(deleteEmploye); // return null ...

        Employe.deleteByID(MatSupprimer);

    }

Upvotes: 0

Views: 267

Answers (3)

Sebastiaan van den Broek
Sebastiaan van den Broek

Reputation: 6331

deleteById is not a static method of the entity (in your case Employe) itself, but a method of the repository.

Try employeRepository.deleteById(theId); or if you already have the entity anyway, just employeRepository.delete(Matsupprimer);

Also as said in the comment, I recommend making your variable Matsupprimer start with lowercase.

Upvotes: 1

Hiren Gohel
Hiren Gohel

Reputation: 1

In your case Employe is hibernate entity. So you couldn't find deleteByID() method in your Employe entity class

Upvotes: 0

Swarit Agarwal
Swarit Agarwal

Reputation: 2648

@Sebastiann is correct you need to use `employeRepository.deleteById(theId), but for that you should have id, while you have mentioned

System.out.println(deleteEmploye); // return null ...

what does value contains of MatSupprimer in Employe deleteEmploye = employeRepository.findByMatricule(MatSupprimer);

employeRepository.deleteByID(MatSupprimer) must looking for a long type id or delete entity object itself

Upvotes: 0

Related Questions