Hibernate自定义删除查询多行

时间:2017-10-12 12:44:45

标签: java mysql hibernate

休眠自定义查询还是新手。

我的桌子是

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

我的控制器检索正确的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

1 个答案:

答案 0 :(得分:0)

我认为您缺少 @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);

是的,这种方法可以删除零行,一行或多行。