繁体   English   中英

JPQL在数组查询中的位置

[英]JPQL where in array query

我正在尝试更新我在arraylist中拥有id的每条记录但是我收到此错误:

IllegalStateException occured : org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [update models.UserOnline uo SET currentRoom_id = :roomid where uo.id IN (:list)]

这就是我正在尝试的:

    Query update_query = JPA.em().createQuery("update UserOnline uo SET currentRoom_id = :roomid where uo.id IN (:list)");
    update_query.setParameter("roomid", null);
    update_query.setParameter("list", idlist);

    List<UserOnline> actual = update_query.getResultList();

有什么想法有什么不对吗?

我会尝试使用update_query.executeUpdate();

来自文档

就像Gonzalo已经说过的那样,你必须使用executeUpdate ()。

这是因为你实际上修改了数据

如果要从数据库中获取数据则只使用getResultList()或getSingleResult()

一个小帮手:如果您的查询有表单,请使用executeUpdate ()

UPDATE ... SET .. WHERE ..
or
DELETE ... WHERE ...

如果查询看起来像是使用getResultList ()或getSingleResult ()

SELECT ... FROM xxx WHERE ... 
or just
FROM xxx WHERE ... 

好吧,如果我们使用Spring Repositories(CrudRepository或其任何类型),并且如果我们有一个带有更新Query的方法声明,那么,

@Query(“update employee e set e.name =:name where e.id =:id”)int updateEmployee(@Param(“name”)String name,@ Param(“id”)Long id);

然后我们将得到相关的Spring Exception org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.hql.internal.QueryExecutionRequestException:不支持DML操作

只需在方法上添加@Modifying注释就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM