skip to Main Content

I found similar questions about this error but I can’t make it work

I’m working on a java 8, spring 2.6.4 and a MySQL database

I’m trying to do a DELETE native query using JPA and I’m getting this error:

org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet

this is my query:

@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
void deleteWhereSlotid(Integer slotid);

and this is the service:

repo.deleteWhereSlotid(reservationSlot.getId());

I’ve also tried:

@Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
Object deleteWhereSlotid(Integer slotid);

//service
Object object= userCourseResRepo.deleteWhereSlotid(reservationSlot.getId());

but it failed

Usually I delete rows with deleteById(id) which comes with spring

The query works though, I tried it on phpMyadmin console and it worked

Someone know what I can try?

4

Answers


  1. If you are using it that way, I believe the query should be:

    DELETE a FROM reservation a WHERE a.slotid =:slotid
    

    I am not particularly sure about the code, however, with Mysql, the case seems to be so when giving an alias to the table.

    Login or Signup to reply.
  2. I know is not the best solution, but you can try to use a query SELECT to find your reservation object and then do this repo.deleteById(reservation.getId())

    This should allow you to go ahead while you find a better way to do it

    Login or Signup to reply.
  3. The way you have it set up, Spring Data assume you want to perform a query (typically a SELECT). For DELETE and similar statements that don’t return a ResultSet you need to provide an additional @Modifying annotation.

    @Modifying
    @Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
    void deleteWhereSlotid(Integer slotid);
    
    Login or Signup to reply.
  4. You need to add @Param

    @Query(value = "DELETE FROM reservation a WHERE a.slotid =:slotid", nativeQuery = true)
    Object deleteWhereSlotid(@Param("slotid")Integer slotid);
    

    As mentioned above, we use the @Param annotation in the method declaration to match parameters defined by name in JPQL/Native query with parameters from the method declaration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search