Tested with Hibernate 3.1.2, java 1.5.0_05
Here is a test case - basically testing session.MERGE () to work correctly, as stated in the docs, but it doesn't according to this test. 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;
}
}
And 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());
}
}
}
}