Java:I';我无法使用crud存储库deleteByID()从我的实体中删除实例
作为OOP的初学者,我在练习中遇到了很多问题。 我想知道,如何使用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
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);
}