我试图用列表作为参数并使用以下条件删除一些行:
@Query("DELETE Entity where col1=:key.val1 and col2 =:key.val2 and col3=:key.val")
@Transactional
@Modifying
public int deleteEntity(@Param("key") List<EntityKey> key );
但是当我尝试使用spring boot启动应用程序时,它会在org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery中返回一个空指针。
有什么办法吗?
答案 0 :(得分:1)
我认为你不能在注释中使用这种方式,你必须创建自己的代码来删除这些元素:
//You should not use OBJECT_NAME.FIELD_NAME in the query
Query query = em.createQuery("DELETE FROM Entity e where e.col1=:val1 "
+ "and e.col2 =:val2 and e.col3=:val3");
//because you have to delete many values, you need a loop
for(EntityKey entity : listEntities){
//set the necessary parameters to the query
query.setParameter("val1", entity.val1);
query.setParameter("val2", entity.val2);
query.setParameter("val3", entity.val3);
query.executeUpdate();//execute update to delete the record
}