-->
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.  [ 2 posts ] 
Author Message
 Post subject: detached objects updated in a new Session
PostPosted: Fri Mar 13, 2009 8:48 am 
Newbie

Joined: Mon Jul 21, 2008 5:40 am
Posts: 16
Hi,

Is what I'm doing correct? basically, I start a transaction, obtain a list of objects, close the session then:

1) I can loop true the list, I assume these are detached objects, they are still accessible
2) I open a new Session and Transaction, then set some properties on the objects in the list obtained from old instance of Session, and use new Session to update, it still works, Am I doing correctly?

Thanks,

Angelo

Transaction tx;
Session session;

session = sessionSource.create();
tx = session.beginTransaction();

Query q = session.createQuery("from Transit");
List lst = q.list();
tx.commit();
session.close();

// accessing detached objects

for (Object o : lst) {
Transit tf = (Transit) o;
}

session = sessionSource.create();
tx = session.beginTransaction();

for (Object o : lst) {
Transit tf = (Transit) o;
tf.setCount(100);
session.update(tf); // why I can update a detached object
}
tx.commit();
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 8:57 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Ofcourse that is fine. This happens in almost all real-worls apps. Usually in one session you loads the object and pass it on to the view layer. The view layer makes some modifications and submits the modified object to the DAO layer. DAO layer now will have to open a new session and a transcation for updating the changes.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.