Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 536
Valentyn Hruzytskyi Avatar asked Jul 09 '18 15:07

Valentyn Hruzytskyi


People also ask

What is difference between JpaRepository and PagingAndSortingRepository?

PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Is JpaRepository deprecated?

Method Summary Deletes the given entities in a batch which means it will create a single query. Deprecated.

What is @query annotation in spring boot?

The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm. xml file. It's a good approach to place a query definition just above the method inside the repository rather than inside our domain model as named queries.


2 Answers

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

like image 121
Alexey Semenyuk Avatar answered Oct 19 '22 04:10

Alexey Semenyuk


In case above solution not work use this

@Modifying
    @Transactional 
    @Query(value ="delete from admindata where user_name = :userName AND group_name = :groupName",nativeQuery = true)
    public void deleteadminUser(@Param("userName") String userName,@Param("groupName") String groupName);
like image 41
Shubham Avatar answered Oct 19 '22 03:10

Shubham