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

标签 java spring jpa

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

@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/

相关文章:

java - 如何恢复文本文件?

java - spring mvc herbinate 与额外列的多对多关系存在问题

java - 如果对象引用是静态的,是否意味着该对象的属性也是静态的?

java - 从 mongodb 结果 java 中删除 _id

java - ehcache RMI 配置到 spring

在更新所有者表时 hibernate 多对多删除连接表中的条目

java - 映射与 Hibernate 的关系

jpa - @OneToOne 是否意味着唯一性?

java - 逐单元读取 .xls 行

java - Spring 数据存储库的问题