开发者

org.hibernate.NonUniqueObjectException

开发者 https://www.devze.com 2023-03-15 23:13 出处:网络
I have an error when I want to delete an object from databas开发者_StackOverflow社区e. The error is:

I have an error when I want to delete an object from databas开发者_StackOverflow社区e. 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 method itself and has more to do with the context in which the delete is called

Check 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.


This error indicates that you have loaded the same object (here same implies that both objects have the same identifier value) more than once in the same session. It doesn't have anything to do with the uniqueness in the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消