net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection

Hibernate JIRA | Frank Pavageau | 1 decade ago
  1. 0

    SubscriberImpl has a lazy map, named emailingProperties: <map name="emailingProperties" table="subscriberemailingproperties" lazy="true" sort="unsorted" inverse="false" cascade="all" > <key column="subscriber" > </key> <index-many-to-many class="com.fullsix.newsletter.impl.EmailingImpl" column="emailing" /> <one-to-many class="com.fullsix.newsletter.impl.BaseSubscriberEmailingPropertiesImpl" /> </map> BaseSubscriberEmailingPropertiesImpl has some properties, including a boolean property named "import_xls". Code between sessionFactory.openSession() and session.close(): I'm just calling session.delete() on an instance of a SubscriberImpl object, which emailingProperties map has not yet been initialized. Here my problem is that my "import_xls" property is not present in the database (this property has been added later, the database has not been altered yet). Of course, hibernate throws an exception (because of an underlying SQLException). However, I would expect an HibernateException here (because Hibernate did not succeeded in deleting my object), instead of a LazyInitializationException (which is the reason why my object didn't get deleted). I think that Hibernate should convert the LazyInitializationException to an HibernateException. Full stack trace of any exception that occurs: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:201) at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71) at net.sf.hibernate.collection.Map.values(Map.java:166) at net.sf.hibernate.type.MapType.getElementsIterator(MapType.java:28) at net.sf.hibernate.engine.Cascades.getAllElementsIterator(Cascades.java:570) at net.sf.hibernate.engine.Cascades.access$100(Cascades.java:28) at net.sf.hibernate.engine.Cascades$1.getCascadableChildrenIterator(Cascades.java:65) at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:525) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482) at net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1237) at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1172) ... 54 more Caused by: net.sf.hibernate.JDBCException: could not initialize collection: [com.fullsix.newsletter.impl.SubscriberImpl.emailingProperties#00000005] at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:287) at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3264) at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195) ... 66 more Caused by: org.postgresql.util.PSQLException: ERROR: column emailingpr0_.import_xls does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:205) at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87) at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:800) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:189) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:915) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:890) at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93) at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284) ... 68 more

    Hibernate JIRA | 1 decade ago | Frank Pavageau
    net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
  2. 0

    SubscriberImpl has a lazy map, named emailingProperties: <map name="emailingProperties" table="subscriberemailingproperties" lazy="true" sort="unsorted" inverse="false" cascade="all" > <key column="subscriber" > </key> <index-many-to-many class="com.fullsix.newsletter.impl.EmailingImpl" column="emailing" /> <one-to-many class="com.fullsix.newsletter.impl.BaseSubscriberEmailingPropertiesImpl" /> </map> BaseSubscriberEmailingPropertiesImpl has some properties, including a boolean property named "import_xls". Code between sessionFactory.openSession() and session.close(): I'm just calling session.delete() on an instance of a SubscriberImpl object, which emailingProperties map has not yet been initialized. Here my problem is that my "import_xls" property is not present in the database (this property has been added later, the database has not been altered yet). Of course, hibernate throws an exception (because of an underlying SQLException). However, I would expect an HibernateException here (because Hibernate did not succeeded in deleting my object), instead of a LazyInitializationException (which is the reason why my object didn't get deleted). I think that Hibernate should convert the LazyInitializationException to an HibernateException. Full stack trace of any exception that occurs: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:201) at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71) at net.sf.hibernate.collection.Map.values(Map.java:166) at net.sf.hibernate.type.MapType.getElementsIterator(MapType.java:28) at net.sf.hibernate.engine.Cascades.getAllElementsIterator(Cascades.java:570) at net.sf.hibernate.engine.Cascades.access$100(Cascades.java:28) at net.sf.hibernate.engine.Cascades$1.getCascadableChildrenIterator(Cascades.java:65) at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:525) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482) at net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1237) at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1172) ... 54 more Caused by: net.sf.hibernate.JDBCException: could not initialize collection: [com.fullsix.newsletter.impl.SubscriberImpl.emailingProperties#00000005] at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:287) at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3264) at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195) ... 66 more Caused by: org.postgresql.util.PSQLException: ERROR: column emailingpr0_.import_xls does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:205) at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87) at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:800) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:189) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:915) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:890) at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93) at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284) ... 68 more

    Hibernate JIRA | 1 decade ago | Frank Pavageau
    net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  4. 0

    When you specify a default_scheme in the hibernate.cfg.xml the sequence generator doesn't respect the default_schema. Instead it uses the sequence in the "public" schema which is what PostgreSQL's schema search defaults to. Here is the generated SQL from when public.items_id_sequence exists: Hibernate: select nextval ('items_id_sequence') Hibernate: insert into beta2.items (name, id) values (?, ?) Note that the first line doens't specify a schema. Here is the generated SQL and following log lines when public.items_id_sequence doesn't exists: Hibernate: select nextval ('items_id_sequence') Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 0, SQLState: 42P01 Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: ERROR: relation "items_id_sequence" does not exist Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 0, SQLState: 42P01 Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: ERROR: relation "items_id_sequence" does not exist Sep 10, 2004 12:07:45 PM net.sf.hibernate.JDBCException <init> SEVERE: Could not save object org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:69) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388) at test.Something.main(Something.java:24) Exception in thread "main" net.sf.hibernate.JDBCException: Could not save object at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:783) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388) at test.Something.main(Something.java:24) Caused by: org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:69) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765) ... 3 more After some debugging I found that the params passed into net.sf.hibernate.id.SequenceGenerator.configure(...) do not contain a property under the "schema" key, which seems to be why SequenceGenerator.generate(..) doesn't know about the schema.

    Hibernate JIRA | 1 decade ago | Sandy McArthur
    org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist
  5. 0

    When you specify a default_scheme in the hibernate.cfg.xml the sequence generator doesn't respect the default_schema. Instead it uses the sequence in the "public" schema which is what PostgreSQL's schema search defaults to. Here is the generated SQL from when public.items_id_sequence exists: Hibernate: select nextval ('items_id_sequence') Hibernate: insert into beta2.items (name, id) values (?, ?) Note that the first line doens't specify a schema. Here is the generated SQL and following log lines when public.items_id_sequence doesn't exists: Hibernate: select nextval ('items_id_sequence') Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 0, SQLState: 42P01 Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: ERROR: relation "items_id_sequence" does not exist Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 0, SQLState: 42P01 Sep 10, 2004 12:07:45 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: ERROR: relation "items_id_sequence" does not exist Sep 10, 2004 12:07:45 PM net.sf.hibernate.JDBCException <init> SEVERE: Could not save object org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:69) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388) at test.Something.main(Something.java:24) Exception in thread "main" net.sf.hibernate.JDBCException: Could not save object at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:783) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388) at test.Something.main(Something.java:24) Caused by: org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50) at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231) at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:69) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765) ... 3 more After some debugging I found that the params passed into net.sf.hibernate.id.SequenceGenerator.configure(...) do not contain a property under the "schema" key, which seems to be why SequenceGenerator.generate(..) doesn't know about the schema.

    Hibernate JIRA | 1 decade ago | Sandy McArthur
    org.postgresql.util.PSQLException: ERROR: relation "items_id_sequence" does not exist

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.postgresql.util.PSQLException

      ERROR: column emailingpr0_.import_xls does not exist

      at org.postgresql.util.PSQLException.parseServerError()
    2. PostgreSQL JDBC Driver
      QueryExecutor.execute
      1. org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139)
      2. org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154)
      3. org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101)
      4. org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
      4 frames
    3. org.postgresql.jdbc1
      AbstractJdbc1Statement.execute
      1. org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515)
      1 frame
    4. PostgreSQL JDBC Driver
      AbstractJdbc2Statement.execute
      1. org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
      1 frame
    5. org.postgresql.jdbc1
      AbstractJdbc1Statement.executeQuery
      1. org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231)
      1 frame
    6. Commons DBCP
      DelegatingPreparedStatement.executeQuery
      1. org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:205)
      1 frame
    7. net.sf.hibernate
      SessionImpl.delete
      1. net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
      2. net.sf.hibernate.loader.Loader.getResultSet(Loader.java:800)
      3. net.sf.hibernate.loader.Loader.doQuery(Loader.java:189)
      4. net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
      5. net.sf.hibernate.loader.Loader.loadCollection(Loader.java:915)
      6. net.sf.hibernate.loader.Loader.loadCollection(Loader.java:890)
      7. net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93)
      8. net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)
      9. net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:287)
      10. net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3264)
      11. net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195)
      12. net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:201)
      13. net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
      14. net.sf.hibernate.collection.Map.values(Map.java:166)
      15. net.sf.hibernate.type.MapType.getElementsIterator(MapType.java:28)
      16. net.sf.hibernate.engine.Cascades.getAllElementsIterator(Cascades.java:570)
      17. net.sf.hibernate.engine.Cascades.access$100(Cascades.java:28)
      18. net.sf.hibernate.engine.Cascades$1.getCascadableChildrenIterator(Cascades.java:65)
      19. net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:525)
      20. net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
      21. net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
      22. net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
      23. net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1237)
      24. net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1172)
      24 frames