Given...
1) A detached object from a previous session that I wish to update().
2) Various business logic validations which must be run (queries, etc.) which may cause the session to contain an object loaded with the same id as from (1). References to these objects are transient, I don't use them beyond the life of the validations. I have no intent of doing update() on the query results.
3) Calling update() on the object from (1) throws NonUniqueObjectException.
How do I deal with this? I've seen the saveOrUpdateCopy() method, jeez that's a bit too much a kitchen sink kinda method than I like to use. I don't even use saveOrUpdate(), if you don't know if you want to create the object or modify the object, you've got larger problems IMO. ;) Our persistence manager layer has 2 methods, insert() and update() that map to Hibernates save() and update() methods. I'd like to keep it simple.
Also, I read this original thread by Gavin, to help me understand what saveOrUpdateCopy() does
http://www.mail-archive.com/hibernate-d ... 02795.html
I was hoping for a simple updateCopy(), without the load it from the database behaviour? Is that capability there and I missed it? Lacking that I was going to trying using Session.contains() and implement my own detection logic and decide how I wanted to deal with the copy issue. However, it does not return true for my detached object when the other instance is in the session. Is there another means I can use to determine if a session contains an object w/ and id, other than load()/get() that will
not go to the database if the object is not in the session?
I considered trying to catch NonUniqueObjectException and deal with it then, but I've not done that as I've been told not to reuse the Session after a HibernateException has occurred. Inconsistent internal state, etc.
Which primitives operations on the Session can I use to deal with this in the fashion I need, and not have to rely on OOTB saveOrUpdateCopy() and that lot?
I'm going to try evict() before I do update() just to avoid this for now, I believe that should do the trick. Only because I need something functional for tomorrow. I don't see that as a long term solution however.
Just found this while searching as well
http://www.hibernate.org/117.html#A23 , maybe evict() is the correct way to go? Kinda does make, since what I'm thinking I want to say is "screw the copy in the session, update the one I'm going to give you here". My only concern might be possible unknown side-effects when cascade is being used.
If anyone can shed more light... Thanks in advance.
--gus