-->
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.  [ 9 posts ] 
Author Message
 Post subject: On detached object support in Hibernate
PostPosted: Tue Dec 09, 2003 9:39 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
I've read HibernateJAOO.ppt and search here for detached object support in Hibernate, but still unclear about its use. The following code will throw Lazy Exception when run:

Code:
public class TestCode {
    public static void main(String[] args) {

        Session session = null;
        try {
            SessionFactory sf = DataStore.getHibernateSessionFactory();
            session = sf.openSession();
            Test test = null;
            Set objectiveQuestions = null;

            test = (Test) session.get(Test.class, new Long(2));
            session.close();
            System.err.println("session closed and will open a new one ");
           
            session = sf.openSession();
            System.err.println(test.getEntryDate());
            System.err.println(test.getTitle());
            Set objectiveTestRequirements = test.getTestRequirementsObjective();
            for (Iterator iterator = objectiveTestRequirements.iterator(); iterator.hasNext();) {
                TestRequirementObjective objectiveTestRequirement = (TestRequirementObjective) iterator.next();
                System.err.println(objectiveTestRequirement.getCatalog().getName());
                objectiveQuestions = objectiveTestRequirement.getObjectiveQuestions();
            }
            session.close();
            System.err.println("session closed and will open a new one ");

            session = sf.openSession();
            for (Iterator iterator = objectiveQuestions.iterator(); iterator.hasNext();) {
                QuestionObjective q = (QuestionObjective) iterator.next();
                System.err.println(q.getEntryDate());
            }
            session.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                session.close();
            } catch (HibernateException e) {
                e.printStackTrace(); 
            }
        }
}


But the following code will work correctly when I replace close/open with disconnect and reconnect:

Code:
public class TestCode {
    public static void main(String[] args) {

        Session session = null;
        try {
            SessionFactory sf = DataStore.getHibernateSessionFactory();
            session = sf.openSession();
            Test test = null;
            Set objectiveQuestions = null;

            test = (Test) session.get(Test.class, new Long(2));
            session.disconnect();
            System.err.println("session closed and open a new one ");
            session.reconnect();
            System.err.println(test.getEntryDate());
            System.err.println(test.getTitle());
            Set objectiveTestRequirements = test.getTestRequirementsObjective();
            for (Iterator iterator = objectiveTestRequirements.iterator(); iterator.hasNext();) {
                TestRequirementObjective objectiveTestRequirement = (TestRequirementObjective) iterator.next();
                System.err.println(objectiveTestRequirement.getCatalog().getName());
                objectiveQuestions = objectiveTestRequirement.getObjectiveQuestions();
            }
            session.disconnect();
            System.err.println("session closed and open a new one ");
            session.reconnect();
            for (Iterator iterator = objectiveQuestions.iterator(); iterator.hasNext();) {
                QuestionObjective q = (QuestionObjective) iterator.next();
                System.err.println(q.getEntryDate());
            }
            session.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                session.close();
            } catch (HibernateException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
        }
}


So does "detached objects support" means I must never close a session (session.close()) ?


Thank you very much !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 11:45 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
You may reassociate an unmodified detached object with a new session by using the lock() method:
session.lock(test, LockMode.NONE)

mota


Top
 Profile  
 
 Post subject: How about the modified objects ?
PostPosted: Tue Dec 09, 2003 11:56 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
For modified objects, what shall I do to be detached.


Top
 Profile  
 
 Post subject: Nobody ever use this feature of Hibernate ?
PostPosted: Wed Dec 10, 2003 3:23 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
Gavin,

Could you tell me how to make use of "detached objects support" in Hibernate please ?

thanks a lot !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
For unmodified objects: session.lock(o, LockMode.NONE)
For modified objects: session.update(o)


Top
 Profile  
 
 Post subject: Am I right ?
PostPosted: Wed Dec 10, 2003 3:48 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
When using session.close() to close a session, all the objects loaded by this session become transient and the associated session cache is destroyed. When using session.disconnect() to close a session, all the objects loaded by this session are still persistent and the associated session cache is not destroyed. So the latter case will be much more efficient and I can still use objects loaded by previous session to get associated collections without any problem. Am I right ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ummm ... welll


yes, you are right but:

(1) destroying the session cache is not slow (a session is lightweight)
(2) you have to find someplace to "keep" the session
(3) you can't do this is a multitiered architecture where objects must be serialized between tiers
(4) be VERY careful about keeping a session open for longer than a single transaction (don't do it unless you really grok the notion of an "application transaction")


Top
 Profile  
 
 Post subject: Thank you very much !
PostPosted: Wed Dec 10, 2003 4:05 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
Than you, Gavin :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 8:38 am 
Newbie

Joined: Mon Sep 13, 2004 8:20 am
Posts: 7
Location: Poland
Hello

I've read many threads considering detached objcets and NonUniqueObjectException but i couldnt find answer to my problem

Gavin wrote:

gavin wrote:
For unmodified objects: session.lock(o, LockMode.NONE)
For modified objects: session.update(o)


but what if i have to load a list of objects (in the same session) to check few things before session.update() and in the list there is also object to be updated. In this case i have "a different object with the same identifier value was already associated with the session" and cannot perform sesson.update() How to menage such situation in 1 session ?


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