gpt4 book ai didi

Spring JPA update "expecting SELECT query"(Spring JPA更新“期待SELECT查询”)

转载 作者:bug小助手 更新时间:2023-10-25 13:36:21 62 4
gpt4 key购买 nike



Here's the query in my repository interface:

下面是我的存储库界面中的查询:


@Modifying
@Lock(LockModeType.PESSIMISTIC_FORCE_INCREMENT)
@Query("UPDATE TradeTransaction tt set tt.externalStatus = :status where tt.id = :id")
int updateTransactionExternalStatus(@Param("status") TradeTransaction.ExternalStatus status,
@Param("id") Long id);

And here's the error:

错误是这样的:


org.hibernate.query.IllegalSelectQueryException: Expecting a SELECT Query [org.hibernate.query.sqm.tree.select.SqmSelectStatement], but found org.hibernate.query.sqm.tree.update.SqmUpdateStatement [UPDATE TradeTransaction tt set tt.externalStatus = :status where tt.id = :id]

Everything I'm reading says the @Modifying annotation should do it.

我读到的所有内容都说@Modify注释应该可以做到这一点。


EDIT: I've determined I don't need this kind of transaction control after all. TomekK's answer may be helpful to someone so I'll leave this up.

编辑:我已经确定我根本不需要这种事务控制。TomekK的答案可能会对某人有所帮助,所以我就把这个留下来吧。


更多回答

Maybe wrong @Modifying annotation (check the package)?

也许@修改注解是错误的(检查包)?

one Question: WHICH Library du you import for the @Query annotation? Are your shure that you import the right one?

一个问题:您为@Query注释导入了哪个库?您确信您进口的是正确的产品吗?

优秀答案推荐

Locking with the use of @Lock doesn't work with non select @Query.
It looks like a bigger theme since we have this in the Query interface documentation:

使用@Lock进行锁定不适用于非SELECT@Query。它看起来像是一个更大的主题,因为我们在查询接口文档中有这样的内容:



Throws: IllegalStateException - if the query is found not to be a Java
Persistence query language SELECT query or a Criteria API query



https://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#setLockMode(javax.persistence.LockModeType)

Https://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#setLockMode(javax.persistence.LockModeType)



Make sure to import the correct @Modifying annotation. Use the one from jpa and not jdbc.

确保导入正确的@Modiving注解。使用来自JPA而不是JDBC的代码。


import org.springframework.data.jpa.repository.Modifying;

导入org.springframework.data.jpa.repository.Modifying;


更多回答

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