-->
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.  [ 7 posts ] 
Author Message
 Post subject: lazy="true"
PostPosted: Mon Sep 20, 2004 10:26 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
Hibernate version:
2.1.3

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name = "escola.Turma" table = "turmas">
<id name = "id" column = "id" unsaved-value = "null">
<generator class = "increment" />
</id>
<property name = "obs" column = "obs" />
<property name = "autor" column = "autor" />
<property name = "ciclo" column = "ciclo" />
<property name = "leI" column = "leI" />
<property name = "leII" column = "leII" />
<property name = "q1" column = "q1" />
<property name = "q2" column = "q2" />
<property name = "q3" column = "q3" />
<property name = "q4" column = "q4" />
<property name = "q5" column = "q5" />
<property name = "q6" column = "q6" />
<property name = "q7" column = "q7" />
<property name = "q8" column = "q8" />
<property name = "q16" column = "q16" />
<property name = "q18" column = "q18" />
<property name = "q25" column = "q25" />
<property name = "q26" column = "q26" />
<property name = "q27" column = "q27" />
<property name = "q28" column = "q28" />
<property name = "q29" column = "q29" />
<property name = "q30" column = "q30" />
<property name = "q31" column = "q31" />
<property name = "q32" column = "q32" />
<property name = "q33_1" column = "q33_1" />
<property name = "q33_2" column = "q33_2" />
<property name = "q33_3" column = "q33_3" />
<property name = "q33_4" column = "q33_4" />
<property name = "q33_5" column = "q33_5" />
<property name = "q33_6" column = "q33_6" />
<property name = "q33_7" column = "q33_7" />
<property name = "q33_8" column = "q33_8" />
<property name = "q33_9" column = "q33_9" />
<property name = "q33_10" column = "q33_10" />
<property name = "q33_11" column = "q33_11" />
<property name = "q33_12" column = "q33_12" />
<property name = "q33a_1" column = "q33a_1" />
<property name = "q33a_2" column = "q33a_2" />
<property name = "q33a_3" column = "q33a_3" />
<property name = "q33a_4" column = "q33a_4" />
<property name = "q33a_5" column = "q33a_5" />
<property name = "q33a_6" column = "q33a_6" />
<property name = "q33a_7" column = "q33a_7" />
<property name = "q33a_8" column = "q33a_8" />
<property name = "q33a_9" column = "q33a_9" />
<property name = "q33a_10" column = "q33a_10" />
<property name = "q33a_11" column = "q33a_11" />
<property name = "q33a_12" column = "q33a_12" />
<property name = "q35" column = "q35" />
<property name = "q36" column = "q36" />
<property name = "q37" column = "q37" />
<property name = "q38" column = "q38" />
<property name = "q41_EE" column = "q41_EE" />
<property name = "q41_Al" column = "q41_Al" />
<property name = "q42" column = "q42" />
<property name = "q43" column = "q43" />
<property name = "q44" column = "q44" />
<property name = "q45_1" column = "q45_1" />
<property name = "q45_2" column = "q45_2" />
<property name = "q45_3" column = "q45_3" />
<property name = "q45_4" column = "q45_4" />
<property name = "q45_5" column = "q45_5" />
<property name = "q45_6" column = "q45_6" />
<property name = "q45_7" column = "q45_7" />
<property name = "q47" column = "q47" />
<property name = "q48" column = "q48" />
<property name = "q49" column = "q49" />
<property name = "q50" column = "q50" />
<property name = "q51" column = "q51" />
<property name = "q51a" column = "q51a" />
<property name = "q52" column = "q52" />
<property name = "q53" column = "q53" />
<property name = "q54" column = "q54" />
<property name = "q55" column = "q55" />
<property name = "q56" column = "q56" />
<property name = "q57" column = "q57" />
<property name = "q58" column = "q58" />
<property name = "q59" column = "q59" />
<property name = "q60" column = "q60" />
<property name = "q60a" column = "q60a" />
<property name = "q61" column = "q61" />
<property name = "q62" column = "q62" />
<property name = "q74" column = "q74" />
<property name = "q76" column = "q76" />
<property name = "q77" column = "q77" />
<property name = "q78" column = "q78" />
<property name = "q79" column = "q79" />
<property name = "q80" column = "q80" />
<property name = "q82" column = "q82" />
<property name = "q83" column = "q83" />
<property name = "q84" column = "q84" />
<property name = "q86" column = "q86" />
<property name = "q87" column = "q87" />
<property name = "q88" column = "q88" />
<property name = "q89" column = "q89" />
<property name = "q90" column = "q90" />
<property name = "q91" column = "q91" />
<property name = "q93" column = "q93" />
<property name = "q94" column = "q94" />
<set name = "q9" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q9" />
</set>
<set name = "q10" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q10" />
</set>
<set name = "q12" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q12" />
</set>
<set name = "q17" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q17" />
</set>
<!--<set name = "q19" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q19" />
</set>-->
<set name = "q34" lazy = "true" cascade = "all-delete-orphan">
<key column = "turma_id" />
<one-to-many class = "escola.Q34" />
</set>
<set name = "q22DiscAl"
lazy = "true"
cascade = "all-delete-orphan"
order-by = "disciplina">
<key column = "turma_id" />
<one-to-many class = "escola.Q22DiscAl" />
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
public void setTudo() throws HibernateException {
Session sess = Utilitarios.currentSession();
LeLista ll = new LeLista();
/*
if ( (Porteiro.oano != 0) && (!Porteiro.aturma.equals("")) &&
(Porteiro.aepoca != 0)) {
ut = Utilitarios.getUtilizador(Porteiro.oano, Porteiro.aturma,
Porteiro.aepoca);
//System.out.println("turma da query: " + p.aturma);

}
*/
//***
if ( (Porteiro.oano != 0) && (!Porteiro.aturma.equals("")) &&
(Porteiro.aepoca != 0)) {
ut = Porteiro.oUtilizador;
//System.out.println("ut: " + ut.getRelatorio().getTurma().getQ2());
}

//***
else {
ut = Utilitarios.getUtilizador(ll.oAno( (String) visual.
jListTurmas.getSelectedValue()),
ll.aTurma( (String) visual.
jListTurmas.
getSelectedValue()), getSel());
}

// povoa label turma
String turma = ut.getRelatorio().getTurma().getQ2() + "


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 11:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you need to have the session opened when hitting the lazy initialised class or association...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 11:43 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
tx for your answer, Antony, but i'm afraid i didnt get it, because, even if i open session before hitting the set, i get the same error msg except if i also make:
session.update(ut);
well, this way it works; so i insist: is it safe to do it the way i do?
thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
be sure to understand what detached object mean.

1- you get your object from a session --> all is fine, lazy will work
2- you close the session --> bug if you try to access a lazy loaded propety --> your object is detached
3- if you open a new session, be sure to reattach the object by calling session.update or session.lock --> that's why your case is working.

Managing your session is the most important

PS: i don't have time to read your java code, so i can be wrong...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 12:49 pm 
Newbie

Joined: Thu Jun 24, 2004 2:19 am
Posts: 10
the Object containing the lazy collection needs to be associated with a session. so just having 'an' open session is not good enough.

you can call Session.lock(yourObject, LockMode.NONE) to re-associate your object with a session, then access the lazy set and then close the session.


pascal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 1:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
as said in the previous post...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 2:44 pm 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
anthony wrote:
be sure to understand what detached object mean.

1- you get your object from a session --> all is fine, lazy will work
2- you close the session --> bug if you try to access a lazy loaded propety --> your object is detached
3- if you open a new session, be sure to reattach the object by calling session.update or session.lock --> that's why your case is working.

Managing your session is the most important

PS: i don't have time to read your java code, so i can be wrong...

1a - i create the object ut in lets call it A.Class and store it in a static var
Code:
static Utilizador oUtilizador;
//...
String query = "SELECT ut from Utilizador as ut "
            + "WHERE ut.palavraPasse = :palavraPasse "
            + "AND ut.nomeUtilizador = :nomeUtilizador "
            + "AND ut.relatorio.turma.q1 = :epocaActual";
        Query q = sess.createQuery(query);
        q.setString("palavraPasse", palavraPasse);
        q.setString("nomeUtilizador", nomeUtilizador);
        q.setInteger("epocaActual", epocaActual);
        List lista = q.list();
        for (int i = 0; i < lista.size(); i++) {
            ut = (Utilizador) lista.get(i);
            if (lista.size() > 0) {
  //...
                    oUtilizador = ut;
  //...

1b - then, in B.class i make
Code:
utlizador = A.oUtilizador;
        Iterator iter2 = ut.getRelatorio().getTurma().getQ22DiscAl().iterator();
        while (iter2.hasNext()) {
            visual.lmQ22Disc.addElement(iter2.next());
        }

In the meanwhile i'm certain no session was closed!
So to Anthony's point #3, i only had to open a new session in order to re.atach, because i figure i allready had (allways) an opened session...

Perhaps with this treatment i use i no longer have a session (at least one that's associated with my ut object)

So i understand Pascal's saying:
Quote:
the Object containing the lazy collection needs to be associated with a session. so just having 'an' open session is not good enough.


Right?
thanks a lot to both for helping me!


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