gpt4 book ai didi

java - 使用 org.springframework.data.jpa.repository.Query 使用列表参数有条件删除

转载 作者:行者123 更新时间:2023-12-02 12:05:40 26 4
gpt4 key购买 nike

我试图删除一些以列表作为参数的行,并满足以下条件:

@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 处返回一个空指针。

有什么办法吗?

最佳答案

我认为您不能在注释中使用这种方式,您必须创建自己的代码来删除此元素:

//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
}

关于java - 使用 org.springframework.data.jpa.repository.Query 使用列表参数有条件删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46926904/

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