java – org.h2.jdbc.JdbcSQLException:方法仅允许查询
发布时间:2020-12-15 04:33:41 所属栏目:Java 来源:网络整理
导读:我使用以下代码在我的数据库上运行查询. @Repositorypublic interface PurchaSEOrderRepository extends JpaRepositoryPurchaSEOrder,PurchaSEOrderID { @Query(value ="update PURCHASE_ORDER set status='REJECTED' where id=?1",nativeQuery = true) void
我使用以下代码在我的数据库上运行查询.
@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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |