Tested with Hibernate 3.1.2, java 1.5.0_05
Here is  a test case - basically testing to see if session.MERGE () works correctly, as stated in the docs.  The test shows that it doesn't!!! From the docs, the session.merge(object) function should do this:
Quote:
The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().
The code works as shown right now
 here is the problem:
     if I comment out sess.merge() I get     org.hibernate.NonUniqueObjectException
     if enable sess.merge() and I comment out sess.saveOrUpdate()
     It doesn't save!!! 
So here is the mapping file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test_merge.TestObj" table="TestObj">
<id name="to_id" 
    column="to_id" 
    unsaved-value="null">
            <generator class="hilo">
                <param name="table">to_seq</param>
                <param name="column">next</param>
            </generator>
            
</id>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
Java code for TestObj:
Code:
package test_merge;
/**
 * @author jt_1000
  */
public class TestObj {
    Long to_id=null;
    String name=null;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
  
   public Long getTo_id() {
      return to_id;
   }
   public void setTo_id(Long to_id) {
      this.to_id = to_id;
   }
}
The part about thread is irrelevant, it does the same thing when working in the current thread. 
scenario 1:
(session->load->copy object ->close session; new session -> merge(copy)->saveOrUpdate [throws NonUniqueObjectException])
scenario 2:
 (session->load->copy object ->close session; new session -> merge(copy)->commit [nothing is saved])
scenario 3:
 (session->load->copy object ->close session; new session -> saveOrUpdate (copy)->commit [saved correctly, only because unsaved-value="null", on ID])
If I put hasCode and/or equals on TestObj still the same results from each of the above scenarios.
I have other reasons to want to get the merge working, since I noticed that collections of children from a parent will behave differently when modifing the collection item (add/delete) and modifying an item, for each situation - if in the session already it will behave correctly with <set inverse="true"... if not in the session that behavior (add/delete items and modify item) doesn't work properly. It only works properly by doing the saveOrUpdate on the parent and the <set ...inverse="false"> (collection managed by parent.) - It works, but with one caveate - it doesn't handle not-null on the FK in the child very well, and won't do delete-orphan.
Here is the Java code that excercises the Java/Mapping:
Code:
package test_merge;
import com.rhinosystemsinc.hibernate.Util.HibernateUtil_V3;
import java.util.*;
import org.hibernate.*;
public class Main {
   public static void copyObject(TestObj to,TestObj tocopy)
   {
      tocopy.setName(new String(to.getName()));
      tocopy.setTo_id(new Long(to.getTo_id().longValue()));
      
   }
   public static void main(String args[]) {
      final SessionFactory sf = HibernateUtil_V3.getSessionFactory(
            "hibernate.properties", new Class[] { TestObj.class });
      final TestObj toCopy=new TestObj();
      //simulate some other request by a thread that loads
      //the object that I am trying to save from the main thread 
      //simulated by the copyObject function.
      Thread t=new Thread(){
         public void run()
         {
            Session sess = sf.openSession();
            System.out.println("first session id=" + sess.hashCode());
            TestObj to=null;
            to = (TestObj)sess.load(TestObj.class,new Long(196608));
            to.setName("object 1");
            copyObject(to,toCopy);
            sess.close();   
         }
      };
      t.start();
      System.out.println("started thread");
      try {
         System.out.println("wait on thread");
         t.join(); //wait for thread to finish.
      } catch (InterruptedException e) {
         System.out.println("E=" + e.getMessage());
         e.printStackTrace();
      }
      System.out.println("continue");
      
      /**
The code works as shown right now
 here is the problem:
 if I comment out sess.merge() I get   org.hibernate.NonUniqueObjectException
 if enable sess.merge() and I comment out sess.saveOrUpdate()
 It doesn't save. 
       */
      Session sess = sf.openSession();
      sess=sf.openSession();
      System.out.println("new session id=" + sess.hashCode());
      //sess.merge(toCopy);
      Transaction tx=sess.beginTransaction();
      //change the detached object.
      toCopy.setName("change: " + new Date());
      sess.saveOrUpdate(toCopy);
      tx.commit(); 
      Query q = sess.createQuery("from TestObj");
      java.util.List result = q.list();
      //sess.close();
      java.util.Iterator itr = null;
      TestObj l = null;
      if (result != null) {
         itr = result.iterator();
         while (itr != null && itr.hasNext()) {
            l = (TestObj) itr.next();
            System.out.println("id=" + l.getTo_id());
            System.out.println("name=" + l.getName());
         }
      }
   }   
}