Spring mvc 在spring项目JpaRepository中删除
我正试图从我的数据库中删除一些员工 但我不能。 这是我的存储库代码:Spring mvc 在spring项目JpaRepository中删除,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我正试图从我的数据库中删除一些员工 但我不能。 这是我的存储库代码: public interface EmployeRepository extends JpaRepository<Employe, Long> { @Query("delete from Employe e where e.idEmploye=:x") public Employe deleteEmploye(@Param("x") int idEmploye); } 当我测试我的控制器时,我得到这个消息 {"t
public interface EmployeRepository extends JpaRepository<Employe, Long> {
@Query("delete from Employe e where e.idEmploye=:x")
public Employe deleteEmploye(@Param("x") int idEmploye); }
当我测试我的控制器时,我得到这个消息
{"timestamp": 1495688812536,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "org.hibernate.hql.internal.QueryExecutionRequestException: Not
supported for DML operations [delete from org.st.entities.Employe e where
e.idEmploye=:x]; nested exception is java.lang.IllegalStateException:
org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for
DML operations [delete from org.st.entities.Employe e where
e.idEmploye=:x]",
"path": "/employe3"
}使用Spring数据JPa,默认情况下只能检索/插入新记录。所以,如果您想要修改/更新/删除现有记录,那么您必须将您的方法标记为@Transactional&@Modifying,以指示Spring给定的方法可以随时更改现有记录。所以 试试这个:
public interface EmployeRepository extends JpaRepository<Employe, Long> {
@Transactional
@Modifying
@Query("delete from Employe e where e.idEmploye=:x")
public Employe deleteEmploye(@Param("x") int idEmploye);
}
公共界面EmployeeRepository扩展了JpaRepository{
@交易的
@修改
@查询(“从Employe中删除,其中e.ideemploye=:x”)
公共雇员删除雇员(@Param(“x”)intideemploye);
}
使用Spring数据JPa,默认情况下只能检索/插入新记录。所以,如果您想要修改/更新/删除现有记录,那么您必须将您的方法标记为@Transactional&@Modifying,以指示Spring给定的方法可以随时更改现有记录。所以
试试这个:
public interface EmployeRepository extends JpaRepository<Employe, Long> {
@Transactional
@Modifying
@Query("delete from Employe e where e.idEmploye=:x")
public Employe deleteEmploye(@Param("x") int idEmploye);
}
公共界面EmployeeRepository扩展了JpaRepository{
@交易的
@修改
@查询(“从Employe中删除,其中e.ideemploye=:x”)
公共雇员删除雇员(@Param(“x”)intideemploye);
}
可以更改数据和@Transactional
的查询。可以更改数据和@Transactional
的查询。这可能正常,但我收到另一个错误,因为“employees”的id是另一个表中的外键消息:无法删除或更新父行:外键约束失败(t\u异常
traitement
,CONSTRAINTfkafuchse28bfyo9jy5cggxqlybx
外键(id\u employe
)引用employe
(id\u employe
)@在本例中,首先您必须删除其子记录,如上例中的“message”表,然后您的父记录“employee”它可能正在工作,但我收到另一个错误,因为“employees”的id是另一个表中的外键消息:无法删除或更新父行:外键约束失败(t\u异常
traitement
,CONSTRAINTfkafuchse28bfyo9jy5cggxqlybx
外键(id\u employe
)引用employe
)。在这种情况下,首先必须删除它的子记录,就像上面的“message”表一样,然后删除它的父记录“employee”