Problem adding an element to a collection at runtime

Asked

Viewed 166 times

0

In this my method I must fill a Collection, regardless of its type

public void loadBeanCollectionItems(B bean, Collection collection, JSONArray collectionJson) 
{
    for(int index = 0; index < collectionJson.length(); index++)
    {
        collection.add(getBean(collectionJson.getJSONObject(index)));
    }
}

but the following exception is generated:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:399)
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:314)
at tools.json.AbstractBeanConverter.loadBeanCollectionItems(AbstractBeanConverter.java:224)
at tools.json.AbstractBeanConverter.loadBeanArray(AbstractBeanConverter.java:215)
at tools.json.AbstractBeanConverter.getBeanArray(AbstractBeanConverter.java:194)
at tools.json.AbstractBeanConverter.loadBeanFields(AbstractBeanConverter.java:177)
at tools.json.AbstractBeanConverter.loadBean(AbstractBeanConverter.java:156)
at tools.json.AbstractBeanConverter.getBean(AbstractBeanConverter.java:142)
at tools.json.AbstractBeanConverter.loadBeanFields(AbstractBeanConverter.java:174)
at tools.json.AbstractBeanConverter.loadBean(AbstractBeanConverter.java:156)
at tools.json.AbstractBeanConverter.getBean(AbstractBeanConverter.java:142)
at tools.json.AbstractBeanConverter.loadBeanCollectionItems(AbstractBeanConverter.java:224)
at tools.json.AbstractBeanConverter.loadBeanArray(AbstractBeanConverter.java:215)
at tools.json.AbstractBeanConverter.getBeanArray(AbstractBeanConverter.java:194)
at tools.json.AbstractBeanConverter.loadBeanFields(AbstractBeanConverter.java:177)
at tools.json.AbstractBeanConverter.loadBean(AbstractBeanConverter.java:156)
at tools.json.AbstractBeanConverter.getBean(AbstractBeanConverter.java:142)
at services.StartGame.doPost(StartGame.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

In this stack of exceptions the shortcut "Abstractbeanconverter.java:224" references the following line of this method:

collection.add(getBean(collectionJson.getJSONObject(index)));
  • 1

    By the way you use Ibernate, it may be because you are initiating the variable with fetch=Lazy, try initializing with fetch=Ager. I won’t answer the answers because I’m not sure, but that could be it, if I format a straight answer.

1 answer

1


This is a problem of using Hibernate. You are accessing a Lazy Collection outside the Hibernate session.

For example, you searched for an object A which has a Lazy Collection of B. But before accessing a.getB() you closed the Hibernate session. When finally a.getB() is invoked, the Hibernate Lazy Collection when trying to retrieve the list of Bs, identifies that there is no more session and lets go this exception.

Actually your problem is adding an element to the collection, but the concept is the same.

You need to find out why your session is not active while running this method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.