-->
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.  [ 3 posts ] 
Author Message
 Post subject: Detached Object and Using Merge
PostPosted: Thu Aug 05, 2010 2:21 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I am trying to use merge function, as explained in the following link.
http://www.hibernate-training-guide.com/merge.html

I wrote the following code.
Code:
      Session session = factory.openSession();
      Transaction t = session.beginTransaction();
      Event event = null;
      try {
         event = (Event) session.load(Event.class, id);
         System.out.println(event.getId());
         //event.setName("hai");
         t.commit();
      }
      catch(Exception e)
      {
         t.rollback();
      }
      finally {
         
         session.close();
      }
      
      Session session2 = factory.openSession();
      Transaction t2 = session2.beginTransaction();
      
      Event event2 = null;
      try {
         event2 = (Event) session2.load(Event.class, id);
         System.out.println(event.getId());
         //event.setName("hai");
         session2.update(event);
         t2.commit();
      }
      catch(Exception e)
      {
         System.out.println(e);
         t2.rollback();
      }
      finally {
         
         session2.close();
      }
      


I do not get exception when i try to run the above code. But instead i am getting message : reassociated uninitialized proxy.
I was hoping that when i try to execute the following statment session2.saveOrUpdate(event);
I would get UniqueOjbectException.

Please advise.


Top
 Profile  
 
 Post subject: Re: Detached Object and Using Merge
PostPosted: Thu Aug 05, 2010 3:20 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
Sometimes I also get following error while running the above code
Code:
12:48:07,963 ERROR LazyInitializationException: - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
   at com.manning.hq.ch03.Event$$EnhancerByCGLIB$$22f2df5d.toString(<generated>)


Top
 Profile  
 
 Post subject: Re: Detached Object and Using Merge
PostPosted: Fri Aug 06, 2010 5:17 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
In order to get the desired exception i added following line
Code:
System.out.println(event2.getName());

instead of, while working on second transaction.
Code:
System.out.println(event.getId());


The reason is that event.getId() only make use of the proxy(the unique identifier are loaded with proxy). When I use event2.getName(), the object for given id is actually loaded in session.

After that when i try to save the event object i get NonUniqueObjectException.


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

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.