-->
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.  [ 8 posts ] 
Author Message
 Post subject: Proxy and Query
PostPosted: Fri Nov 21, 2003 7:20 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
session = fac.openSession();
Query q = new Query("from MyClassWithProxy ...");
session.close();

Iterator it = q.iterate();
while (it.hasNext()) {
MyClassWithProxy c = (MyClassWithProxy) it.next();
c.getAtribute() ///// Exception initializing proxy .. net.sf.hibernate.HibernateException: Could not initialize proxy (session closed)
}

If my class doesn't use a proxy I can iterate the results without problem even after close the session, otherwise if my class uses a proxy the class will be loaded only when I call an accessor method, in this case during the iteration but my session is closed ...

Its possible to initialize the class with proxy before start to iterate it ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Are you sure, non proxied class works ?
Since Hibernate requests DB for each line when using iiterate (and object no in session or jvm cache), I'm skeptical.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 8:14 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Yes, it works ...
Code:
Session session = null;
        try {
            session = factory.openSession();

            query = session.createQuery("from Empresa as e where :user not in elements (e.users) ");
            query.setParameter("user",model,Hibernate.entity(com.estobel.model.User.class));

                   
        } catch (HibernateException e) {
            reporter.showErrorMessage("Erro: "+e);
            return;

        } finally {
            session.close();
        }

//Here I Iterate ... works fine when Empresa isn't proxied.



Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 8:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You're telling me that

open session
create query
close session
execute query

works, there is something I missed : I must go to bed.

Code:
try {
            session = factory.openSession();
            query = session.createQuery("from Empresa as e where :user not in elements (e.users) ");
           query.setParameter("user",model,Hibernate.entity(com.estobel.model.User.class));                   
        } catch (HibernateException e) {
            reporter.showErrorMessage("Erro: "+e);
            return;
        } finally {
            session.close();
        }
        terator it = q.iterate();  //no error here ???
        while (it.hasNext()) {

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 9:00 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
nvolpini, Emmanuel is right - the code you show here can not work even if you were not using proxies because you close session before Hibernate is able to perform the first SELECT. Obviously, you are showing us not the real code but some fragments of it (I suppose manually typed here). For example, Query is INTERFACE but you managed to compile "new Query(...)" somehow... If you prefer you problem was solved quickly, your better show the real code which actually fails isntead of some "transcript". (No offence, this is just my personal expirience)

Anyway. About your question: docs says: "Note that find() does not return proxies.". I suppose if you use session.find() instead of iterate(), everything should work fine. But make sure, you calling find() BEFORE you close session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 10:01 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Something is wrong with my code ... help to understand the log for:

Code:
Session session = null;
        try {
            session = factory.openSession();

            query = session.createQuery("from Empresa as e where :user not in elements (e.users) ");
            query.setParameter("user",model,Hibernate.entity(com.estobel.model.User.class));

                   
        } catch (HibernateException e) {
            reporter.showErrorMessage("Erro: "+e);
            return;

        } finally {
            factory.closeSession(session);
        }

java.util.Iterator it = query.iterate();

while (it.hasNext()) {
   Group obj = (Group) it.next();
    ....
}


The log for the code ...

[code]
23:31:38,977 DEBUG HibernateFactory:? - Abrindo sess


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 4:43 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Hm... I see no errors in your log.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 9:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The request executed is
Code:
from Group as e where :user not in elements (e.users)

not
Code:
from Empresa as e where :user not in elements (e.users)


Thus
Code:
query = session.createQuery("from Empresa as e where :user not in elements (e.users) ");

is never executed.

try to replace HibernateException with Exception in your catch statement

_________________
Emmanuel


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