gpt4 book ai didi

java - Hibernate自定义删除查询多行

转载 作者:行者123 更新时间:2023-11-28 23:07:54 24 4
gpt4 key购买 nike

hibernate 自定义查询仍然是新手。

我的 table 是

ID   TID   R1   Position
1 1 1 2
2 1 1 3

我想要一个自定义查询来删除 TID 为 1 和 R1 1 的行

我现在的sql是这样的

@Query(value = "delete from Table t where t.TID= :tid and t.R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(@Param("tid") Integer TID, @Param("r1") Integer r1);

它给我以下错误:

.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

我的 Controller 检索正确的 tid 和 r1 ID。甚至可以一次删除多行吗?我的错误可能在哪里?

编辑

在我将@Modyfying 添加到查询后我得到了错误

TransactionRequiredException: Executing an update/delete query

在我将@Transactional 与@Modifying 结合使用后,我得到了

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't where t.tid= 1 and t.R1 = 99' at line 1

最佳答案

我认为您缺少 @Modifying 注释,指示修改数据库的查询:

@Modifying
@Query(value = "delete from Table where TID= :tid and R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(@Param("tid") Integer TID, @Param("r1") Integer r1);

是的,此方法可以删除零行、一行或多行。

关于java - Hibernate自定义删除查询多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46709958/

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