I have an error when I want to delete an object from database. The error is:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.cartif.database.ApplicationField#asd]
To delete an object I do:
public static void delete(Object o){
if(session == null) createSession();
Transaction tx = session.beginTransaction();
tx.begin();
session.delete(o);
tx.commit();
}
When I call this method with an object, I obtain the error. If I debug the application the exception is thrown in tx.begin(); line.
On database this object is unique, as I show in the columns:
name deviceid
"asd" 1
"ElectricalConsumption" 1
"Energy" 1
Why is that happened?
Thanks in advance!
NonUniqueObjectException is thrown when there is an object already associated with the session with the same id (primary key) as the one you are trying to associate with session.
This generally has nothing to do with the delete
methoditself and has more to do with the context in which the delete is calledCheck the place where the delete is called. Check for any possible duplicate objects in the place where the delete is called.
This question may also help.