英文:
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<Reservation,String> {
@Query("UPDATE Reservation SET Status =:Status WHERE pnr =:pnr")
String setStatus( @Param("Status")String Status,@Param("pnr") String pnr);
}
Error
> org.springframework.dao.InvalidDataAccessApiUsageException: Expecting
> a SELECT query : UPDATE Reservation SET Status = :Status WHERE pnr =
at Caused by: org.hibernate.query.IllegalSelectQueryException:
> :pnr
> 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<Reservation,String> {
@Modifying
@Transactional
@Query("UPDATE Reservation SET Status =:Status WHERE pnr =:pnr")
void setStatus( @Param("Status")String Status,@Param("pnr") String pnr);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论