-->
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: Failed to lazily initialize a collection -- session closed??
PostPosted: Fri Jul 01, 2005 5:59 am 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
Hibernate version: 2.1.6

Mapping documents: The one in the problem: Persona.hbm.xml:
Code:
<hibernate-mapping>

  <class name="it.unitn.science.crisaune.db.Persona" table="persona">

    <id name="id" type="integer" unsaved-value="null">
      <column name="id_persona" not-null="true"/>
      <generator class="native"/>
    </id>
    <property    name="nome"          column="nome"        type="string"/>
    <property    name="cognome"       column="cognome"       type="string"/>
    <!-- email ha valore UNIVOCO, gestito dall'applicazione -->
    <property    name="email"         column="email"         type="string"/>
    <property    name="password"      column="password"      type="string"/>
    <property    name="telefono"      column="telefono"      type="string"/>
    <property    name="telefono2"     column="telefono2"     type="string"/>
    <property    name="ruolo"         column="ruolo"         type="string"/>
    <many-to-one name="labDapparten"  column="lab_dapparten" class="it.unitn.science.crisaune.db.Laboratorio"/>
    <property    name="dataRegistraz" column="data_reg_pers" type="date"/>

    <set name="partecipazioni" inverse="false" order-by="id_partecipazione desc">
      <key column="id_partecipazione"/>
      <one-to-many class="it.unitn.science.crisaune.db.Partecipazione"/>
    </set>

    <set name="iscrizioni" inverse="false" lazy="false" order-by="id_iscrizione desc">
      <key column="id_iscrizione"/>
      <!--index column="id_iscrizione"/-->
      <one-to-many class="it.unitn.science.crisaune.db.Iscrizione"/>
    </set>

    <set name="privilegi" table="autorizzazioni" lazy="false">
      <key column="persona"/>
      <!--index column="id_autorizz"/-->
      <many-to-many column="privilegio" class="it.unitn.science.crisaune.db.Privilegio"/>
    </set>

  </class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
I'm using JSF and this is the problem: find how open- and closeSession could must be done.
Code:
            HSessions.currentSession();
            iscrizioni = persona.getIscrizioni();
            iscrizioni.size();

p.s.: HSessions is one of my own classes. It's a factory for hibernate sessions. It returns a new session or the same if it was already opened.

For each browser request I open the session, make my work and close the session. I save a object representing the person in the session of webserver. Nothing problem in the other pages, but in this backing bean, used in only one page, there are these rows in the constructor, that cause LazyInitializationException (iscrizioni.size() causes this exception).
I don't understand why Hibernate can't load the set iscrizioni from database if I have reopened the session. It's a problem if I close and reopen the session for each request?? I can't mantain session opened while I wait the client to request again... because the data in db will not be saved... and other users don't read news...
I'm confused... anyone can clear my errors or give me more light on the problem?
thanx very much!
cloud

Full stack trace of any exception that occurs:
Code:
javax.servlet.ServletException: #{iscrizione.aggiungi}: javax.faces.el.EvaluationException: net.sf.h
ibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or sessi
on was closed
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:247)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at it.unitn.science.crisaune.filters.HibernateFiltro.doFilter(HibernateFiltro.java:28)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:213)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at it.unitn.science.crisaune.filters.AutenticazioneFiltro.doFilter(AutenticazioneFiltro.java
:40)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:213)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:641)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:641)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11P
rotocol.java:700)
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
        at java.lang.Thread.run(Thread.java:534)


Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true): too long, pages and pages...

Debug level Hibernate log excerpt: sorry, I don't know what it is.

tanx....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 7:26 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
Hello to Italy, czanon,

I have had the same problem once and as far as I have read it is necessary for lazy initialization that the same Hibernate session is used that created the object holding the collection.
Lazy initialization is not possible with other sessions opened later.

It is dangerous to store objects in the HTTP Session that make use of lazy initialization. In your case you have set lazy = false and you should not have a problem...
Maybe your problem lies in the class="it.unitn.science.crisaune.db.Iscrizione". Try to set all <properties> of that class lazy = false.

Does that help?

Kai


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 8:10 am 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
Kuwe wrote:
It is dangerous to store objects in the HTTP Session that make use of lazy initialization. In your case you have set lazy = false and you should not have a problem...

Oh... ok, I have set lazy=false each set that I have to use later in the HTTP session, when the (current) Hibernate Session will have been closed (I'm sorry for my Eglish ;o) ) and so all works.
So I forgot lazily loading in these situations. thanks for the precision ;o)
bye!
...from Italy! a bit of links ;o) http://www.termecomano.it - http://www.trentino.to

czanon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 8:56 am 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
But... another question, near to the first one...

I obtain a NonUniqueObjectException loading an object from the db that I have already loaded but it is associated to a
previous session, that I closed.

In particular I have a backing bean that loads 'persona' (the one that have iscrizioni set (see above)):
First of all I open the session,
I load Persona's instance:
Code:
Session s = HSessions.currentSession();
Query q = s.createQuery("FROM Persona p"+" WHERE p.email=:login AND p.password=:pass");
q.setString("login", loginNome);
q.setString("pass", loginPassword);
Iterator it = q.iterate();
if (it.hasNext()) persona = (Persona) it.next();

then I close the Session.
When the user invokes the action add Iscrizione (a subscription), I RELOAD Persona to save it correctly with new set in which
now there is a fresh subscription:
Code:
Iscrizione isc = new Iscrizione(iscritto, dataIscriz, lavoro, chi, persona);
Session session = HSessions.currentSession();
session.load(persona, persona.getId()); // <--- this line causes the exception
persona.getIscrizioni().add(isc);
session.update(persona);

but I'm not able to load persona because this exception happens:
Code:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was alr
eady associated with the session: 1, of class: it.unitn.science.crisaune.db.Persona
        at net.sf.hibernate.impl.SessionImpl.doLoadByObject(SessionImpl.java:1973)
        at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1915)
        at it.unitn.science.crisaune.backing.iscrizioni.ModuloIscrizione.aggiungi(ModuloIscrizione.j
ava:76)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
        at javax.faces.component.UICommand.broadcast(UICommand.java:312)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
        at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:247)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at it.unitn.science.crisaune.filters.HibernateFiltro.doFilter(HibernateFiltro.java:28)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:213)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at it.unitn.science.crisaune.filters.AutenticazioneFiltro.doFilter(AutenticazioneFiltro.java
:40)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:213)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:641)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:641)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(Standar
dPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
        at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11P
rotocol.java:700)
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
        at java.lang.Thread.run(Thread.java:534)

What does "was already associated with the session" mean? I think I don't understand this.
If I newly load the object using the id I previously get, why I can't obtain it again? If I can't use the old
one instance anymore because the old session was close, why I can't load a new instance?
I can't understand where is the problem...

czanon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 12:36 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
Hi Again,

It seems that you create a 2nd referenze to the persona object.
Try not to load persona from you DB. If you have it stored in the HTTP Session take this instance and work with it.

Gruss
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 12:58 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
Hint: You might even consider not to store the persona object in your HTTP Session.
Just store User and Password in the HTTP Session and reload the persona object every time you need it.
That avoids you a lot of Problems and should not influece your performance.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 02, 2005 10:18 am 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
Kuwe wrote:
Hint: You might even consider not to store the persona object in your HTTP Session.
Just store User and Password in the HTTP Session and reload the persona object every time you need it.
That avoids you a lot of Problems and should not influece your performance.


ok, thanx for your response. I'll try on this way next week.
I'll write here soon...

bye,
czanon


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.