2

In my project, some time I have to load a object to edit them. Before this, I think, hibernate session is closed.

Some time before save the edited object (with saveOrUpdate), I have to search (open session again) for a object in the database, and, maybe, the object returned by this search is the object I'll update. (I need this object for some validations and other things...)

After this, when I try to save the object, hibernate says that I have another object with the same identifier in the session.

I have implemented my own equals and hashcode methods, and I tried to do somethings, like session.evict(object), but, nothing works.

So, I use session.merge(object) instead of session.saveOrUpdate(object), and it works. But, I dont know exactly what is the consequences of doing this. I can simple replace saveOrUpdate by merge, or I will get some issues with it? Another thing I have to know about this?

Thanks in advance.

caarlos0
  • 20,020
  • 27
  • 85
  • 160

1 Answers1

1

i think this is a duplicate for What are the differences between Hibernate Session methods saveOrUpdate() and merge()? and a number of other questions, very common question indeed...

Community
  • 1
  • 1
jambriz
  • 1,273
  • 1
  • 10
  • 25
  • yeah, maybe. But, I still dont understand what exactly merge do. Like, "yeah, it merge the objects", but, if the name of a Person object, for example, is "Carlos", and I updated it to "jambriz", what of them will be persisted, and why? Other thing, what happens with the cascades? – caarlos0 Jan 05 '12 at 17:20