You need to complete the modification by @Modifying annotations during the project (note: New is not supported)
1. Before a business needs to query a result set and then make a change to a property value in another table that satisfies a condition in the result set, this change may be multiple
The code is as follows
The 2.SQL statement is as follows
3. But in the process of execution, the console error, as follows
Can not issue data manipulation statements with executeQuery ().
4. @Modifying annotations were later found to be not added
5. Talk about the actual use of @modifying annotations
For example, a test
@Modifying @Query ("update person Set email =: Email where lastName =:lastname")
void Updatepersonemailbylastname (@Param ("LastName") string lastname,@Param ("email") string email);
Execute an error, the exception is as follows,
The key looks at this line Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
, which means we are missing a transaction configuration.
You can add a thing annotation above the service implementation class, or you can add a thing annotation above the method, or add the management of the service thing controller in the XML file
@Service
@Transactional
<context:component-scan base-package="Com.liwei.springdata.service" ></Context: component-scan>
Summarized as follows
(1) UPDATE and DELETE operations can be completed via a custom JPQL. Note: JPQL does not support the use of INSERT;
(2) Write JPQL statements in @Query annotations, but must be decorated with @Modifying. To notify Springdata that this is an UPDATE or DELETE operation
(3) The UPDATE or DELETE operation requires a transaction, at which point the service layer is defined and transaction operations are added to the service layer's methods;
(4) By default, Springdata has a transaction on each method, but is a read-only transaction. They cannot complete the modification operation.
@modifying Annotations of SPRING-DATA-JPA