Announcement Announcement Module
No announcement yet.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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

    [ERROR] 11.05.2011 22:51:30 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(
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(
    [ERROR] 	at org.hibernate.collection.AbstractPersistentCollection.initialize(
    [ERROR] 	at
    [ERROR] 	at org.hibernate.collection.PersistentBag.iterator(
    [ERROR] 	at
    [ERROR] 	at$200(
    [ERROR] 	at$1.visitReferenceProperty(
    [ERROR] 	at
    [ERROR] 	at
    [ERROR] 	at
    [ERROR] 	at

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

    			    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));
    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
    mvn gwt:run
    Import the test.xml file
    Last edited by cf1337; May 11th, 2011, 09:26 PM. Reason: deleted link - down

  • #2

    exactly this line:

    				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