IllegalSelectQueryException和InvalidDataAccessApiUsageException

huangapple go评论112阅读模式
英文:

IllegalSelectQueryException and InvalidDataAccessApiUsageException

问题

我在我的项目中使用Query注释。当我调用查询时,我收到IllegalSelectQueryException错误。我想要根据PNR号码存在的情况来更新Reservation表上的Status列。

@Repository
public interface ReservationRepository extends JpaRepository<Reservation, String> {
    @Query("UPDATE Reservation SET Status = :Status WHERE pnr = :pnr")
    String setStatus(@Param("Status") String Status, @Param("pnr") String pnr);
}

错误信息:

org.springframework.dao.InvalidDataAccessApiUsageException: 期望一个SELECT查询:`UPDATE Reservation SET Status = :Status WHERE pnr = :pnr`,
但找到的是org.hibernate.query.sqm.tree.update.SqmUpdateStatement [UPDATE Reservation SET Status = :Status WHERE pnr = :pnr]
  at ...

我尝试更改查询,但没有任何效果。

英文:

I use Query annotation for my project. While I call query, I get IllegalSelectQueryException. I want to update status column which is on Reservation table with WHERE the PNR number exists.

@Repository
public interface ReservationRepository extends JpaRepository&lt;Reservation,String&gt; {
    @Query(&quot;UPDATE Reservation SET Status =:Status WHERE pnr =:pnr&quot;)
    String setStatus( @Param(&quot;Status&quot;)String Status,@Param(&quot;pnr&quot;) String pnr);
}

Error

> org.springframework.dao.InvalidDataAccessApiUsageException: Expecting
> a SELECT query : UPDATE Reservation SET Status = :Status WHERE pnr =
&gt; :pnr
at Caused by: 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
> Reservation SET Status = :Status WHERE pnr = :pnr] at
> org.hibernate.query.sqm.internal.SqmUtil.verifyIsSelectStatement(SqmUtil.java:81)
> ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final] at
> org.hibernate.query.sqm.internal.QuerySqmImpl.verifySelect(QuerySqmImpl.java:499)
> ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final] ... 102 common frames
> omitted

I tried changing the query but nothing works

答案1

得分: 2

@org.springframework.data.jpa.repository.Modifying注解添加到方法中,并将返回值设置为void,因为更新查询不能返回一个值。

@Repository
public interface ReservationRepository extends JpaRepository<Reservation, String> {
    @Modifying
    @Transactional
    @Query("UPDATE Reservation SET Status = :Status WHERE pnr = :pnr")
    void setStatus(@Param("Status") String Status, @Param("pnr") String pnr);
}
英文:

Add the @org.springframework.data.jpa.repository.Modifying annotation to the method and set the return value to void,because update queries can not return a value

@Repository
public interface ReservationRepository extends JpaRepository&lt;Reservation,String&gt; {
    @Modifying
    @Transactional
    @Query(&quot;UPDATE Reservation SET Status =:Status WHERE pnr =:pnr&quot;)
    void setStatus( @Param(&quot;Status&quot;)String Status,@Param(&quot;pnr&quot;) String pnr);
}

huangapple
  • 本文由 发表于 2023年7月17日 21:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定