Announcement Announcement Module
Collapse
No announcement yet.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.hibernate.LazyInitializationException: failed to lazily initialize a collection

    AGAIN - web is full of these, but none covers my problem.

    How comes?
    I have several datastructures like Container (Class with a list) and Content (Contentclass of the list with some values)

    This is the expected behaviour when compiling from xsd schemata - i think

    So i want these xml-related objects to be persisted in my database.
    Currently this is a HYPERSONIC_IN_MEMORY

    All classes are Annotated with their @Xml and @JPA-Annotations.

    Additionally i used spring-ROO (latest version from git) to scaffold my applcation with Google Web Toolkit
    Plus a Importer-Widget to get Data into the database.

    Now - as soon as i imported a Container and its Contents into the GWT-Application, i cannot access "Containers" anymore

    Code:
    [ERROR] 11.05.2011 22:51:30 com.google.gwt.requestfactory.server.RequestFactoryServlet doPost
    [ERROR] SCHWERWIEGEND: Unexpected error
    [ERROR] org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: de.test.server.domain.Container.ct, no session or session was closed
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    [ERROR] 	at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    [ERROR] 	at com.google.gwt.requestfactory.server.Resolver.resolveClientValue(Resolver.java:441)
    [ERROR] 	at com.google.gwt.requestfactory.server.Resolver.access$200(Resolver.java:49)
    [ERROR] 	at com.google.gwt.requestfactory.server.Resolver$1.visitReferenceProperty(Resolver.java:359)
    [ERROR] 	at com.google.gwt.autobean.shared.AutoBeanVisitor.visitCollectionProperty(AutoBeanVisitor.java:165)
    [ERROR] 	at com.google.gwt.autobean.server.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:251)
    [ERROR] 	at com.google.gwt.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:153)
    [ERROR] 	at com.google.gwt.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:112)

    Weird thing is, this appears as soon, as i import data from xml. The code snippet:

    Code:
    			    byte[] fileContents = uploadItem.get();
    				ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
    			    MyMarshaller as = ctx.getBean("myMarshaller", MyMarshaller.class);
    			    Container mb = as.importContainer(new String(fileContents));
    			    System.out.println(mb.toString());
    			    mb.persist();
    This persists (at least i think so) also the generated Content-Objects.
    I can also access the Content-Objects from the GWT-App.

    I broke the project down into a minimal example.
    Please try my code and give feedback. Could this a scffolding project with roo?
    Just unzip the file into a fresh folder and run
    Code:
    mvn gwt:run
    Import the test.xml file
    Last edited by cf1337; May 11th, 2011, 09:26 PM. Reason: deleted link - down

  • #2
    Solved!

    exactly this line:

    Code:
    				ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
    generates a new Context and messes up the Application.

    One should use WebApplicationContext generating an springContext from HttpServlet's getContext() method

    Comment

    Working...
    X