gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:15:58 25 4
gpt4 key购买 nike

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

我的运行者

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

}

最佳答案

deleteById 不是实体(在您的情况下 Employe)本身的静态方法,而是存储库的方法。

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

正如评论中所说,我建议您的变量 Matsupprimer 以小写字母开头。

关于Java:我无法使用 CRUD 存储库 deleteByID() 从我的实体中删除实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58988405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com