What is the difference between the persist method and the merge method in Jpa

In the background, the project currently being done has two tables for children and parents, and the relationship between one child and many parents. The primary key id is self-incrementing

If you need to add a new parent to an existing child, org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.kids.pt.entity.Kid;

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.kids.pt.entity.Kid

The exception information is that you need to save a kid, the id is not null when the save() method is called, because the database will automatically increment the primary key when the id is manually configured and saved, so an error will occur.

Then in the project dao, I found that saveOrUpadate() was not available, and then Baidu found that the persist() method and merge() method in the original jpa are similar to the relationship between save() and saveOrUpadate().

/*The following content is from Baidu*/////////////////////////////////////// ///////////////////////////////////////////// ///////////

persist(), is to save, like the save() method, knowledge jpa officially said that it is better to call persist, which is closer to the meaning of persistence. And merge() means merge, that is, when the entity you save is divided according to the primary key id, if it already exists, it is an update operation, and if it does not exist, it is a new operation.

persist will put the passed in entity into the persistence context. If there is this entity in the persistence context, javax.persistence.EntityExistsException will be thrown. If not, the object will be added to the database when the transaction is submitted. If that object (that row) already exists in the database, a com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException will be thrown;

and merge will generate a managed version of the passed-in entity in the persistence context, if it already exists If there is a managed version, it will not throw an exception, and then return the managed entity. When the transaction commits, if the object (that row) does not exist in the database, add the managed one to exist. If so, replace the original data. Merge is to update if there is a managed version in the persistence context, if not, copy a copy and return the managed version.

Summarize persist again (①, ②-③, ④-⑤):
(The exceptions thrown here refer to the repeated exceptions of objects (or rows in the database))
① If persist is a managed entity (that is, it has been context), no exception will be thrown.
2 If the persist is a free entity (that is, there is no context in it), and there is no managed version of it in the context, nor in the database, and no exception will be thrown, but the entity will be written into the database.
3 If the persist is a free entity (that is, not in the context), and there is no managed version of it in the context, but the database has this entity, then the EntityManager will not throw an exception when persisting it, but when the transaction commits An exception will be thrown:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '7' for key 1;
④If the persistent entity is a free entity (that is, not in the context), but the context has its managed version, If there is no such entity in the database, then it will not throw an exception, but add its managed version (not the one that is free, that is the one that is managed!) (ie, persist is the same as no persist in this case of!).
⑤ If the persist is a free entity (that is, not in the context), but there is a managed version of it in the context, and the entity also exists in the database, then the EntityManager will throw an exception when persisting it: javax.persistence. EntityExistsException
and merge will not throw any duplicate exceptions. .












{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324108123&siteId=291194637