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