我发现了关于这个错误的类似问题,但我不能使它工作
我正在开发java 8, spring 2.6.4和MySQL数据库
我正在尝试使用JPA做一个DELETE本地查询,我得到这个错误:
org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet;嵌套异常是org.hibernate.exception.GenericJDBCException: could not extract ResultSet
这是我的查询:
@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
void deleteWhereSlotid(Integer slotid);
,这是服务:
repo.deleteWhereSlotid(reservationSlot.getId());
我也试过了:
@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
Object deleteWhereSlotid(Integer slotid);
//service
Object object= userCourseResRepo.deleteWhereSlotid(reservationSlot.getId());
但是失败了
通常我会删除带有deleteById(id)
的行,它带有spring
查询工作虽然,我尝试了它在phpMyadmin控制台,它工作
有人知道我可以试试什么吗?你设置的方式,弹簧数据假设您想执行一个查询(通常是SELECT
)。对于不返回ResultSet
的DELETE
和类似语句,您需要提供额外的@Modifying
注释。
@Modifying
@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
void deleteWhereSlotid(Integer slotid);
我知道这不是最好的解决方案,但您可以尝试使用查询SELECT来查找您的预订对象,然后执行此repo.deleteById(reservation.getId())
当你找到更好的方法时,这应该允许你继续进行
如果您这样使用,我认为查询应该是:
DELETE a FROM reservation a WHERE a.slotid =:slotid
我对代码不是特别确定,但是,对于Mysql,当给表一个别名时,情况似乎是这样。
您需要添加@Param
@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
Object deleteWhereSlotid(@Param("slotid")Integer slotid);
如上所述,我们在方法声明中使用@Param
注释来匹配JPQL/Native查询中由名称定义的参数与方法声明中的参数。