我有两个要在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错误,该错误是:执行更新/删除查询
尝试使用以下内容进行更新查询
@Transactional
@Modifying(clearAutomatically = true)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句