Four main methods of JPA Entitymanager--persist,merge,refresh and REMOVE_SPRING-DATA-JPA

Source: Internet
Author: User
Tags flush

public void persist (Object entity)

The Persist method can convert an instance to a managed (managed) state. After the flush () method is invoked or the object is submitted, the instance is inserted into the database.

A,persist of instances in different states will produce the following actions:

1. If a is an entity of a new state, it will be converted to a managed state;

2. If a is an entity with a managed state, its state will not change in any way. However, the system will still perform an insert operation on the database;

3. If a is a removed (delete) state entity, it will be converted to a controlled state;

4. If a is an detached (detached) State entity, the method throws a IllegalArgumentException exception, which is related to the different JPA implementations. public void merge (Object entity)

The main role of the merge method is to archive the user's modifications to a detached state entity, and a new managed state object will be created after the archive.

A,merge of instances in different states will produce the following actions:

1. If a is a detached state entity, the method submits a modification to the database and returns an instance of the new managed state A2;

2. If a is an entity of a new state, the method produces a managed state entity A2 based on A;

3. If a is an entity with a managed state, its state will not change in any way. However, the system will still perform an update operation on the database;

4. If a is an removed state entity, the method throws a IllegalArgumentException exception. public void Refresh (Object entity)

The Refresh method guarantees that the current instance is consistent with the contents of the instance in the database.

A,refresh of instances in different states will produce the following actions:

1. If a is an instance of a new state, no action occurs, but it is possible to throw an exception, depending on the different JPA implementations;

2. If a is an instance of a managed state, its properties will be synchronized with the data in the database;

3. If a is an instance of a removed state, no action will occur;

4. If a is an detached state entity, the method throws an exception. public void Remove (Object entity)

The Remove method can convert an entity to a removed state and delete the data in the database after calling the flush () method or submitting the object.

A,remove of instances in different states will produce the following actions:

1. If a is an instance of a new state, the state of a will not change, but the system will still execute the DELETE statement in the database;

2. If a is an instance of a managed state, its state is converted to removed;

3. If a is an instance of a removed state, no action will occur;

4. If a is an detached state entity, the method throws an exception.

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.