0

Possible Duplicate:
Spring + Hibernate : a different object with the same identifier value was already associated with the session

I have the following object (simplified) as a JavaBean:

public class Person {
  private Integer id;
  private City cityOfBirth;
  private City city;
  // ...
}

And in my spring form I have 2 select combos in order to choose both cities, as follows:

<form:form method="post" action="" commandName="person">
City of birth: <form:select path="cityOfBirth" items="${ cities }" itemValue="id" />
City: <form:select path="city" items="${ cities }" itemValue="id" />
...
</form:form>

My PropertyEditor for the City class would simply call to my CityDao's get, wich is the following:

@Component
public class CityDaoImpl implements CityDao {
  private @Autowired HibernateTemplate hibernateTemplate;

  public City get (Integer id) {
    return (City) hibernateTemplate.get(City.class, id);
  }
}

And my PersonDao would do this in order to save the instance:

public void save (Person person) {
  hibernateTemplate.saveOrUpdate (person);
}

Everything works OK when I try to save a Person with 2 different cities, but when I choose the same City I obtain the following error:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.project.si.models.common.City#90]

I've read in other posts that this happens because Hibernate Session is currently aware of the previous City obtained when the propertyEditor called cityDao.get(id), so I'm supposed to use merge() somewhere, but I don't get where should I apply this..

Community
  • 1
  • 1
Joaquín L. Robles
  • 6,261
  • 10
  • 66
  • 96

1 Answers1

1

The problem is caused by the fact that you cascade saveOrUpdate() operation from Person to City, and update() fails when object with the same id as the object to be updated is already associated with the Session.

I think the best solution for this problem is to remove cascading options from city and cityOfBirth.

As long as your user have to select a City from a list of existing cities, cascading doesn't make sense here because you don't need to cascade any changes from Person to City.

Alternatively you can use merge() instead of saveOrUpdate():

public void save (Person person) {
    hibernateTemplate.merge(person); 
} 
axtavt
  • 239,438
  • 41
  • 511
  • 482