@modifying Annotations of SPRING-DATA-JPA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.