2

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

I have a problem with Hibernate:

I have the following class: Person

I'm loading a specific Person.class "A" from the db on the server-side. Then I present this object to the client via a JSP page.

The client manipulates "A" to "B" and then sends it back via the form. I load the Person.class "B" object on the server-side into a new variable and then try to store it.

But as the whole application is session based I cannot simply call:

HibernateTemplate.saveOrUpdate("B");

Instead I have to merge "B" with "A" before saving it.

But when I call

HibernateTemplate.merge("B");

the "B" object still gets all the properties of "A" which i would not like to store anymore. I'd like that "B" replaces the "A" object completely without getting some of the "A"-properties by merging them.

So my question is:

Is there in Hibernate any "overwrite" or "replace" function which can overwrite all the objects in a session which have the same identifier with the properties of one specific object?

(In the example the returned object "B" from the form)

It should also prevent Hibernate from complaining about:

Exception in thread "main" org.hibernate.NonUniqueObjectException: a   
different object with the same identifier value was already associated
with the session:
Community
  • 1
  • 1
ndrizza
  • 3,285
  • 6
  • 27
  • 41
  • I'm not sure I understand. If you are trying to replace A with B, then simply delete A from the database and add B. – Randy Stegbauer Jul 25 '12 at 14:04
  • thanks, i have done this previos to my definite solution. that would have worked too. – ndrizza Jul 25 '12 at 16:15
  • Voting to reopen... this question is about deliberately overwriting an object, the other one is about accidentally doing it and how to avoid it – Alex R Nov 04 '18 at 18:12

1 Answers1

1

You could evict the object from the session first and then use update to attach the other object with the session.

I would do this in your service:

sessionFactory.getCurrentSession().evict(myObject);
sessionFactory.getCurrenctSession().update(myNewerObject);

you shouldn't be doing any database operations in your controller since that would require transactional controller methods.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • i've tried to do that in the controller before it's passed to the jsp.. but then the jsp had no access anymore to the object.. at which point would you evict it? – ndrizza Jul 25 '12 at 14:03
  • thanks. i could solve my problem with merge. the problem lied somwhere else in an ArrayList not managed by Hibernate which I had to manage myself.. I did a complete deletion and than insertion of all objects in the list. I thought the mapping would be handled by hibernate... thanks anyways for your help. I can't really tell you now if your code works. – ndrizza Jul 25 '12 at 16:14
  • That's ok. I wouldn't really recommend it to be honest! But we've had to implement something similar to handle some legacy use cases. – Alex Barnes Jul 25 '12 at 17:23