org.hibernate.NonUniqueObjectException: a different object with the same identif

The error org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session appeared in the project.


This phenomenon occurs in my own project:
when an operation object is updated, the front end transmits an entity object, which contains the primary key id, and the background queries the original object information saved in the database according to this primary key, but In the end, the entity object passed from the front end is saved, resulting in two identical but different entities in the same session in hibernate, resulting in calling getHibernateTemplate().saveOrUpdate(o); and reporting an error. or due to improper use.

Solution:
1. When updating, if you need to query the original data object according to the primary key id, then you can use the query result as the final save object.
2. What is said on the Internet is to use getHibernateTemplate().getSessionFactory().getCurrentSession().evict(Object o); after testing, if the updated object is saved, clear the queried object first


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117735&siteId=291194637