5

I'm saving a List by using hibernate, but it throws the following exception:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

The code I'm using is below, but I don't know why it throws the exception:

public void save(List<UserItem> list)
{
    //getHibernateTemplate().saveOrUpdateAll(list);

    //getHibernateTemplate().deleteAll(list);
    sessFactory = getHibernateTemplate().getSessionFactory();
    Session session = sessFactory.getCurrentSession();
    for (UserItem bean : list) {
        session.saveOrUpdate(bean);
    }
}

What is the correct way to save the List?

code_dredd
  • 5,915
  • 1
  • 25
  • 53
Java
  • 85
  • 3
  • 3
  • 9
  • 1
    I've updated your post, but I still think it'd be helpful to others if you include the following information: **1)** Which *specific* line throws the exception? (Step through the code to find out if needed.) **2)** Did you read the javadocs for the method throwing? Under what circumstances does it claim to throw, if at all? What was unclear? **3)** What other things have you tried that might provide more info? Is an [MCVE](http://stackoverflow.com/help/mcve) feasible so that others can reproduce? If so, post one. – code_dredd Oct 08 '16 at 10:36

2 Answers2

2

The problem is that an object with that id already exists in the session, using merge will fix all of your problems but you should really look into the differences. Just copy this and it'll work.

public void save(List<UserItem> list)
{
    //getHibernateTemplate().saveOrUpdateAll(list);

    //getHibernateTemplate().deleteAll(list);
    sessFactory = getHibernateTemplate().getSessionFactory();
    Session session = sessFactory.getCurrentSession();
    for (UserItem bean : list) {
        session.merge(bean);
    }
}

Here's a good source for more info on the hibernate persistence related methods What are the differences between the different saving methods in Hibernate?

Community
  • 1
  • 1
SergeiBednar
  • 370
  • 1
  • 10
  • @Java I don't know the logic you're trying to implement, but In short, merge also does save or update. There are more nuances though, check the **answers** on this question for more info: http://stackoverflow.com/questions/161224/what-are-the-differences-between-the-different-saving-methods-in-hibernate – SergeiBednar Oct 08 '16 at 12:05
  • i tried save list in db, i tried this way **getHibernateTemplate().saveOrUpdateAll(list);** list is save but duplicates also saved, i mean one recored shows multiple times. – Java Oct 08 '16 at 12:13
  • my hbm properties ** user_ITEM_ID_SEQ ** – Java Oct 08 '16 at 12:15
  • @Java Is there a problem when you're using the code I posted? – SergeiBednar Oct 08 '16 at 12:19
  • this line is not worked **getHibernateTemplate().saveOrUpdateAll(list);**. so i tried posted code. that is also not working – Java Oct 08 '16 at 12:32
  • @Java what exception are you getting? – SergeiBednar Oct 08 '16 at 12:32
  • org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: – Java Oct 08 '16 at 12:35
  • org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started – Java Oct 08 '16 at 12:36
1

The main reason or cause of this error is, in your list there are objects with the same primary key, it means two object with same primary key but they are not same object instance.

I would suggest you to iterate your list and print your primary key value and find out if is there any object which has same primary key value. Just put print function in your for loop to print your primary key value for each object before calling session.saveOrUpdate(bean); line.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50