When spring data JPA uses spring data JPA, the "delete" and "insert" operations in one method of the service layer are processed within the same transaction

Source: Internet
Author: User

Scene:

Now, one of the things that is provided in the service is to remove all eligible data before inserting all new conditions into the database

This scenario requires a two-step implementation of the service

1. Delete

2. Insert

These two steps are naturally done in the same transaction, which is a complete operation.

So for this scenario, let's see how the annotations work.

1 "First look at the DAO layer link: http://www.cnblogs.com/sxdcgaq8080/p/8984140.html

The DAO layer is also the delete operation of the repository layer, which is the use of the delete operation in JPA, which needs to be added

@Modifying
@Transactional

These two annotations, because

[Email protected] to notify Springdata that this is an UPDATE or DELETE operation

The 2.UPDATE or delete operation requires a transaction, so you need to add these two annotations to the DAO layer's Delete method

2 "and then said the service layer of this method in two steps to go

Needs to be added to the entry method.

@Transactional

Annotations to ensure that two-step operations are performed within the same transaction

In this case, the first transaction annotation worked, and the annotation on the Delete method was added to support the delete operation.

When spring data JPA uses spring data JPA, the "delete" and "insert" operations in one method of the service layer are processed within the same transaction

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.