title: org.hibernate.exception.DataException: could not initialize a collection
body: <p>When I try to access my collection (List) the following way, I get the following error, even though I do it within same UserTransaction.</p>
  
  <pre><code>:::
  EntityAccessorException: org.hibernate.exception.DataException: could not initialize a collection
  :::
  Caused by: org.hibernate.exception.DataException: could not initialize a collection: 
  :::
  
                          List&lt;Brand&gt; brands = fullTextQuery.getResultList();
              for (Brand b : brands) {
  
                  Iterator&lt;BrandPromoDetails&gt; i2 = b.getBrandPromoDetails()
                          .iterator();
  
                  while (i2.hasNext()) {
  
                      BrandPromoDetails bpd = i2.next();
  </code></pre>
  
  <p>====================================================================================</p>
  
  <p>Whereas, when I change it to as follows, it works. My database is Derby and I am using Hibernate as JPA provider and using Hibernate Search. It would be great if anyone can provide an explanation of this behavior.</p>
  
  <pre><code>List&lt;Brand&gt; brands = fullTextQuery.getResultList();
  
                      for (Brand b : brands) {
  
                          @SuppressWarnings("unchecked")
                          List&lt;BrandPromoDetails&gt; i2 = em.createQuery("select x from BrandPromoDetails as x where BPDBRAND_ID = ?1")
                              .setParameter(1, b.getId())
                              .getResultList();
  
                          for (BrandPromoDetails bpd : i2) {
  </code></pre>
  
  <h2>Complete Stacktrace added:</h2>
  
  <p>Here it is..</p>
  
  <pre><code>[#|2011-02-23T22:34:04.711+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=91;_ThreadName=Thread-1;|Hibernate: select brandpromo0_.bpdBrand_id as bpdBrand10_11_1_, brandpromo0_.id as id1_, brandpromo0_.bpdCreationDate as bpdCreat2_1_, brandpromo0_.id as id16_0_, brandpromo0_.bpdBrand_id as bpdBrand10_16_0_, brandpromo0_.bpdCreationDate as bpdCreat2_16_0_, brandpromo0_.bpdPrice as bpdPrice16_0_, brandpromo0_.bpdPriceType as bpdPrice4_16_0_, brandpromo0_.bpdPromoCreatedBy as bpdPromo5_16_0_, brandpromo0_.bpdPromoDescription as bpdPromo6_16_0_, brandpromo0_.bpdPromoName as bpdPromo7_16_0_, brandpromo0_.bpdUnitPrice as bpdUnitP8_16_0_, brandpromo0_.bpdWOMPowerCondition as bpdWOMPo9_16_0_ from WOMPOWER.BrandPromoDetails brandpromo0_ where brandpromo0_.bpdBrand_id=?|#]
  
  [#|2011-02-23T22:34:04.742+0530|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=91;_ThreadName=Thread-1;|29930 [http-thread-pool-8080-(1)] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 20000, SQLState: 22005
  |#]
  
  [#|2011-02-23T22:34:04.742+0530|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=91;_ThreadName=Thread-1;|29930 [http-thread-pool-8080-(1)] ERROR org.hibernate.util.JDBCExceptionReporter - An attempt was made to get a data value of type 'java.sql.Types 91' from a data value of type 'int'.
  |#]
  
  [#|2011-02-23T22:34:04.758+0530|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=91;_ThreadName=Thread-1;|29946 [http-thread-pool-8080-(1)] WARN org.hibernate.engine.loading.LoadContexts - fail-safe cleanup (collections) : org.hibernate.engine.loading.CollectionLoadContext@a27c32&lt;rs=com.sun.gjc.spi.jdbc40.ResultSetWrapper40@184aa30&gt;
  |#]
  
  [#|2011-02-23T22:34:04.758+0530|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=91;_ThreadName=Thread-1;|29946 [http-thread-pool-8080-(1)] WARN org.hibernate.engine.loading.CollectionLoadContext - On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [1] entries
  |#]
  
  [#|2011-02-23T22:34:04.758+0530|SEVERE|glassfish3.0.1|com.wompower.backing.search.SearchRegistry|_ThreadID=91;_ThreadName=Thread-1;|The log message is null.
  com.wompower.entity.accessor.common.EntityAccessorException: org.hibernate.exception.DataException: could not initialize a collection: [com.wompower.entity.brand.Brand.brandPromoDetails#1]
      at com.wompower.entity.accessor.common.AbstractEntityAccessor.doSearchInTransaction(AbstractEntityAccessor.java:137)
      at com.wompower.backing.search.SearchRegistry.getBuySearchResult(SearchRegistry.java:99)
      at com.wompower.backing.search.SearchBacking.buySearch(SearchBacking.java:65)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:613)
      at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
      at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:121)
      at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2345)
      at javax.faces.event.SystemEvent.processListener(SystemEvent.java:102)
      at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:1993)
      at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:1941)
      at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:285)
      at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:243)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:114)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
      at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
      at java.lang.Thread.run(Thread.java:729)
  Caused by: org.hibernate.exception.DataException: could not initialize a collection: [com.wompower.entity.brand.Brand.brandPromoDetails#1]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
      at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
      at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
      at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
      at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
      at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
      at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
      at org.hibernate.collection.PersistentList.iterator(PersistentList.java:138)
      at java.util.AbstractCollection.addAll(AbstractCollection.java:334)
      at com.wompower.backing.search.SearchRegistry$2.execute(SearchRegistry.java:126)
      at com.wompower.backing.search.SearchRegistry$2.execute(SearchRegistry.java:1)
      at com.wompower.entity.accessor.common.AbstractEntityAccessor.doSearchInTransaction(AbstractEntityAccessor.java:121)
      ... 49 more
  Caused by: java.sql.SQLDataException: An attempt was made to get a data value of type 'java.sql.Types 91' from a data value of type 'int'.
      at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
      at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
      at org.apache.derby.client.am.ResultSet.getInt(Unknown Source)
      at org.apache.derby.client.am.ResultSet.getInt(Unknown Source)
      at com.sun.gjc.spi.base.ResultSetWrapper.getInt(ResultSetWrapper.java:459)
      at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:61)
      at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:229)
      at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:768)
      at org.hibernate.collection.PersistentList.readFrom(PersistentList.java:402)
      at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1156)
      at org.hibernate.loader.Loader.readCollectionElements(Loader.java:774)
      at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:622)
      at org.hibernate.loader.Loader.doQuery(Loader.java:829)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
      at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
      ... 60 more
  Caused by: org.apache.derby.client.am.ColumnTypeConversionException: An attempt was made to get a data value of type 'java.sql.Types 91' from a data value of type 'int'.
      at org.apache.derby.client.am.Cursor.getInt(Unknown Source)
      ... 76 more
  |#]
  </code></pre>