Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:I';我无法使用crud存储库deleteByID()从我的实体中删除实例_Java_Spring_Repository - Fatal编程技术网

Java:I';我无法使用crud存储库deleteByID()从我的实体中删除实例

Java:I';我无法使用crud存储库deleteByID()从我的实体中删除实例,java,spring,repository,Java,Spring,Repository,作为OOP的初学者,我在练习中遇到了很多问题。 我想知道,如何使用deleteById()从myrunner.java中删除employe 我的存储库 import com.ipiecoles.java.eval2x0.model.Employe; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort.Direction; import org.springframewo

作为OOP的初学者,我在练习中遇到了很多问题。 我想知道,如何使用
deleteById()
myrunner.java
中删除
employe

我的存储库

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


deleteById
不是实体(在您的案例中是
Employe
)本身的静态方法,而是存储库的方法

尝试
employeerepository.deleteById(theId)
;或者,如果您已经拥有该实体,只需
employeeposition.delete(Matsupprimer)


同样如评论中所述,我建议将变量Matsupprimer以小写字母开头。

@Sebastiann是正确的,您需要使用'employeerepository.deleteById(theId),但是如果您提到了,您应该使用id

System.out.println(deleteEmploye);//返回空值

Employe deleteEmploye=employeRepository.findByMatricule(MatSupprimer)中MatSupprimer的值包含什么内容


EmployeeRepository.deleteByID(MatSupprimer)必须查找long类型id或删除实体对象本身

在您的情况下
Employe
是休眠实体。因此,在
Employe
实体类中找不到
deleteByID()
方法,只是一个提示:按照惯例,变量名应该以小写字母开头。(
MatSupprimer
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);

    }