“ FOR UPDATE NOWAIT”,Oracle,springframework,QuerySyntaxException

时间:2018-10-30 00:54:31

标签: java spring oracle sql-update

我正在使用实现从INSERT INTO fruit_category (fruit_id, cat_id) SELECT f.id, c.id FROM fruit AS f CROSS JOIN category AS c WHERE f.name = 'apple' AND c.name = 'red' org.springframework.data.repository.CrudRepository扩展的接口

我有一个参数为org.springframework.data.jpa.repository.JpaSpecificationExecutor的查询org.springframework.data.jpa.repository.Query

我有这个查询。

org.springframework.data.repository.query.Param

我遇到此错误:

@Query(value = " SELECT c FROM Ctype c WHERE c.code = :code"
        + " FOR UPDATE nowait ")
Ctype findOneByCodeNoWait(
        @Param("code") String code);

我正在检查另一篇文章,但他们使用Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.company.persistence.entity.Ctype org.company.persist.repository.CtypeRepository.findOneByCodeNoWait(java.lang.String)! Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FOR near line 1, column 111 [ SELECT c FROM org.company.persistence.entity.Ctype c WHERE c.code = :code FOR UPDATE nowait ] Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FOR near line 1, column 111 [ SELECT c FROM org.company.persistence.entity.Ctype c WHERE c.code = :code FOR UPDATE nowait ]\"}}" ...

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

@Lock(LockModeType.PESSIMISTIC_READ) @QueryHints(@QueryHint(name = "javax.persistence.lock.timeout",value = "0")) @Query(value = " SELECT c FROM Ctype c WHERE c.code = :code")

我已经在正在进行的项目和以下代码中对此进行了测试:

@Lock(LockModeType.PESSIMISTIC_READ)
@QueryHints(@QueryHint(name = "javax.persistence.lock.timeout",value = "0"))
@Query("select t from Event t left join fetch t.details ")
List<Event> findAllWithDetails();

`

生成类似sql的

SELECT event0_.event_id ... FROM EVENTS event0_ left outer join event_details details1_ ON event0_.event_id = details1_.event_id FOR UPDATE NOWAIT