【问题标题】:Why is the first Query working and the second not? (Spring application JPA)为什么第一个查询有效而第二个查询无效? (Spring应用JPA)
【发布时间】: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 错误,上面写着:执行更新/删除查询

【问题讨论】:

    标签: java sql spring jpa


    【解决方案1】:

    尝试使用下面的更新查询

     @Transactional
     @Modifying(clearAutomatically = true)
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2019-04-16
      • 1970-01-01
      相关资源
      最近更新 更多