JPA无法删除对象【实际项目解决办法】
并非通用, 根据自己实际情况来
不能删除前的dao方法
public void delete(CmsProjectNew bean);
可以删除后的dao方法
@Modifying
@Query("delete CmsProjectNew bean where bean.id in (?1)")
public void deleteById(Integer id);
【下面错误原因】——service方法增加@Transactional
@Transactional
public void delete(int id) {
dao.deleteById(id);
}
严重: Servlet.service() for servlet [background] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query] with root cause
javax.persistence.TransactionRequiredException: Executing an update/delete query