-->
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: Detaching objects
PostPosted: Thu May 08, 2008 4:10 pm 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
This is the body of the update function in the DAO class

org.hibernate.Transaction tx=getSession().beginTransaction();
getSession().update(instance);
tx.commit();

What does it mean by detaching objects and attaching objects?

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 9:56 pm 
Newbie

Joined: Mon Apr 14, 2008 1:13 pm
Posts: 11
A detached object is one that has been persistent, but its Session has been closed.

For example:
Code:
    Session session = factory.openSession();
    MyEntity myEntity = null;
    Transaction tx;
    try {
        tx = session.beginTransaction();
        //do some work
        entity = session.get(MyEntity.class, new Long(12345));
        ...
        tx.commit();
    } catch (Exception e) {
        if (tx!=null) tx.rollback();
        throw e;
    } finally {
        session.close();
    }
    // myEntity is now a detached instance
    System.out.println(myEntity.getFoo());


For more info on different stats of persistent objects, see this page: http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html


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.