gpt4 book ai didi

java - org.h2.jdbc.JdbcSQL异常 : Method is only allowed for a query

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:54 25 4
gpt4 key购买 nike

我已使用以下代码对我的数据库运行查询。

@Repository
public interface PurchaseOrderRepository extends JpaRepository<PurchaseOrder, PurchaseOrderID> {

@Query(value ="update PURCHASE_ORDER set status='REJECTED' where id=?1", nativeQuery = true)
void RejectPO(Long id);
}

然后我简单地在服务中调用这个方法

@Service
public class SalesService {

@Autowired
PurchaseOrderRepository purchaseOrderRepository;
public void RejectPurchaseOrder(Long of) {

purchaseOrderRepository.RejectPO(of);
}
}

但是我遇到了一个错误:

org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
update PURCHASE_ORDER set status='REJECTED' where id=? [90002-191]

问题是,我从未调用过executeQuery,我只是要求使用jpa 运行它。那我该如何解决呢?

最佳答案

为了让 JPA 实际运行修改数据库状态的自定义 @Query,必须使用 @Modifying 注释该方法以告诉 JPA 使用 executeUpdate 等。

代替

@Query(value ="update PURCHASE_ORDER   set status='REJECTED'   where id=?1", nativeQuery = true)
void RejectPO(Long id);

尝试

@Modifying
@Query(value ="update PURCHASE_ORDER set status='REJECTED' where id=?1", nativeQuery = true)
void RejectPO(Long id);

参见 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries了解详情。

关于java - org.h2.jdbc.JdbcSQL异常 : Method is only allowed for a query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563595/

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