# Lazy initialization exception on mass indexing/intial indexing

**URL:** <https://discourse.hibernate.org/t/lazy-initialization-exception-on-mass-indexing-intial-indexing/1471>\
**Category:** Hibernate Search\
**Created:** [September 28, 2018, 9:07pm UTC](https://discourse.hibernate.org/t/lazy-initialization-exception-on-mass-indexing-intial-indexing/1471 "2018-09-28T21:07:48Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [October 1, 2018, 7:30am UTC](https://discourse.hibernate.org/t/lazy-initialization-exception-on-mass-indexing-intial-indexing/1471/2 "2018-10-01T07:30:59Z")

</div>

Hibernate Search is not the one throwing the exception. As you can see here, the error occurs while fetching results from Hibernate ORM:

```auto
|INFO | jvm 1 | 2018/09/28 16:58:11 | |at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1896)|
|---|---|
|INFO | jvm 1 | 2018/09/28 16:58:11 | |at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:366)|
|INFO | jvm 1 | 2018/09/28 16:58:11 | |at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.loadList(IdentifierConsumerDocumentProducer.java:174)|

```

And as you can see here, the error is caused by some problem in the bytecode enhancement:

```auto
INFO | jvm 1 | 2018/09/28 16:58:11 | Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:574)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection.access$200(AbstractPersistentCollection.java:55)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:165)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:146)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143)
INFO | jvm 1 | 2018/09/28 16:58:11 | at java.util.AbstractSet.equals(AbstractSet.java:92)
INFO | jvm 1 | 2018/09/28 16:58:11 | at org.hibernate.internal.util.compare.EqualsHelper.areEqual(EqualsHelper.java:53)
INFO | jvm 1 | 2018/09/28 16:58:11 | at com.csc.pt.svc.data.to.Basclt0100TO.$$_hibernate_write_basclt0300toList(Basclt0100TO.java)
INFO | jvm 1 | 2018/09/28 16:58:11 | ... 49 more
I

```

If I was you, I would try to reproduce the problem with a basic Hibernate ORM query that tries to load entity `Basclt0100TO`. There is very probably either a mapping issue, or a bug in Hibernate ORM. In either case, getting rid of some complexity can only help you to debug it.

As side notes…

First, you’re using outdated versions of Hibernate Search an Hibernate ORM; the latest version of Hibernate Search is `5.10.4.Final` and the latest version of Hibernate ORM is `5.3.6.Final`. Switching to those versions could eliminate your problem, as several bugs have been fixed, in particular when it comes to bytecode enhancement.

Second, even though it’s unrelated to your problem, Hibernate Search 5 is not designed to work well with Elasticsearch 6. From [the documentation](https://docs.jboss.org/hibernate/search/5.9/reference/en-US/html_single/#_elasticsearch_version):

> Hibernate Search expects an Elasticsearch cluster running version 2.x or 5.x.

You probably also got a warning in your logs about that.

Now, as long as ES6 is backward-compatible with ES5, it should work, but since it’s a major version, there’s no guarantee. In particular Elasticsearch 6 does not allow to map multiple types in a single index, so you should avoid doing that.

---

_[View the full topic](https://discourse.hibernate.org/t/lazy-initialization-exception-on-mass-indexing-intial-indexing/1471)._
