-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: testcase: session.merge not working - NonUniqueObjectExcep
PostPosted: Sat Mar 25, 2006 2:10 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
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());
         }
      }
   }   
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.