Spring Jpa Update: Can not issue data manipulation statements with executeQuery()

Valentyn Hruzytskyi :

I try to use handle query for updating table in the SQL database.

Code:

    @Autowired
    private ProducerRepository producerRepository;

    public void update(Producer producer){

        String name = producer.getProducerName();
        long id = producer.getId();

//        producerRepository.save(producer); //this method works well.
        producerRepository.update(name, id); //handle attempt - throws exeption in this string
    }

ProducerRepository:

@Repository
    public interface ProducerRepository extends JpaRepository<Producer, Long>{

        @Query(nativeQuery = true, value = "UPDATE producer SET producer_name = :pName WHERE id = :id")
        Producer update(
                @Param("pName") String pName,
                @Param("id") long id
        );
    }

All parameters of the producer entity are correct and producerRepository.save(producer) works well. (also I out in console name and id fields - all right)

So, I can save producer in the database, but, when I try to use update() method I get the error.

Can not issue data manipulation statements with executeQuery()


PS

sql query in the console also works well

(UPDATE producer SET producer_name = 'some name' WHERE id = ....)

It should be noted that other SQL native queries in repository work correctly. So the spring/hibernate/jdbc settings are correct.

Alexey Semenyuk :

Use annotation @Modifying.

This will trigger the query annotated to the method as updating query instead of a selecting one.

From 2.2.6 Modifying queries https://docs.spring.io/spring-data/jpa/docs/1.3.4.RELEASE/reference/html/jpa.repositories.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84966&siteId=1