【发布时间】:2020-09-02 16:42:55
【问题描述】:
我有两个要在 Spring 项目中使用的查询(使用 JPA)。第一个获得一个帐户,这可以正常工作。对于第二个,我希望它能够更新数据库中的“禁用”字段。代码如下所示:
// This is the first Query (works correctly)
@Query(value = "SELECT * FROM accounts WHERE email = ?1", nativeQuery = true)
Account findByEmailAddress(String emailAddress);
// This is the second Query (doesn't work)
@Modifying
@Query(value = "UPDATE accounts SET disabled = 1 WHERE email= ?1 ", nativeQuery = true)
int disableAccountByEmail(String emailAddress);
我确实在需要添加@Modifying 的地方读到,这将返回一个int 或void。但是当我尝试测试它是否有效时,我得到一个 TransactionRequiredException 错误,上面写着:执行更新/删除查询
【问题讨论】: