0

I am trying to run a simple insert query in spring boot but I am unable to do so.

Query:

@Modifying
    @Query(value = "insert into Local(userId,name,address,pin) VALUES (:userId,:name,:address,:pin)", nativeQuery = true)

    List < Local > insertattributes(@Param("userId")String userId,@Param("address") String address ,@Param("name")String name,@Param("pin") String pin);

Controller:

@RequestMapping("insertattributes/{userId}/{name}/{address}/{pin}")
    @ResponseBody
    public List < Local > insertAttributes(@PathVariable String userId, @PathVariable String name, @PathVariable String address, @PathVariable String pin) {

        return localService.insertattributes(userId,name,address,pin);
    }

Error:

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S1009
o.h.engine.jdbc.spi.SqlExceptionHelper   : Can not issue data manipulation statements with executeQuery().
[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
2
  • Your insertattributes method in repository should be void. It is inappropriate to expect List<Local> during insert Commented Apr 6, 2018 at 6:20
  • I want to cache the response.That is why I was trying to return a List. Commented Apr 6, 2018 at 6:21

1 Answer 1

1

An insert statement doesn't return the inserted record. Since you're expecting a list to be returned executeQuery() is executed by Spring. What you want is the executiong of executeUpdate() which is only executed for void methods.

See also Cannot issue data manipulation statements with executeQuery()

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to fetch a list?
I found the same issue with the update queries too.I understand what you are saying but is there a workaround so that I can get a list or an object?
The question here is what you actually want to do. Is there a need for excuting a native query for your insert? Why don't you create an instance of your Local class and call repository.save() in your JpaRepository? That would also return the saved record for you. See also spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa for reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.