Problem adding an element to a Collection at runtime

0

In this method, I have to fill out 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 "AbstractBeanConverter.java:224" shortcut references the following line of this method:

collection.add(getBean(collectionJson.getJSONObject(index)));
    
asked by anonymous 27.10.2014 / 23:25

1 answer

1

This is a hibernate usage problem. You are accessing a lazy collection outside the hibernate session.

For example, you searched for a A object that has a lazy collection of B . But before accessing a.getB() you have closed the hibernate session. When finally a.getB() is invoked, the lazy collection of hibernate when trying to retrieve the list of Bs, identifies that there is no more session and releases this exception.

In fact, your problem is when you add an element to the collection, but the concept is the same.

You need to find out why your session is not active during the execution of this method.

    
28.10.2014 / 02:38