# Force initializing collection loading

**URL:** <https://discourse.hibernate.org/t/force-initializing-collection-loading/7172>\
**Category:** Hibernate Search\
**Created:** [January 19, 2023, 4:19pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172 "2023-01-19T16:19:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 19, 2023, 4:19pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/1 "2023-01-19T16:19:17Z")

</div>

Today I made another step towards migration end.  
The project is compiled and indexing process starts.  
At some point I get an exception:

> org.hibernate.AssertionFailure: force initializing collection loading

The full indexing log is [here](https://horvoje.net/share/hs6/indexing.log).

Another issue I have is with my `Provider` class I mentioned before.  
Provider’s `name` property is embedded immutable class `LocalizedField` which holds a `Collections::unmodifiableMap` where key is `Locale` and value is translation. This map has its own converter which converts to `JSON` while storing to repository and converts to `Map` when reading from repository.

The reason why I use `JSON` and not let Hibernate do it is to avoid creating additional tables for translations (which would happen because of `Map`) and also to be able to use json\_search that gives me ability to implement autocomplete. I would do it with Lucene if there were option for wildcard search. My whole Map is converted to single field:

```auto
{
	"hr_HR" : "Mrkvica - Ulica grada Mainza",
	"en_US" : "Mrkvica - The street of city of Mainz"
}

```

**LocalizedField.java**

```auto
@NoArgsConstructor @ToString @EqualsAndHashCode
public class LocalizedField implements Serializable {

	private static final long serialVersionUID = -7002190313077107236L;

	@Convert( converter = LocaleStringMapConverter.class )
	private Map<Locale, String> data;

	public LocalizedField( final LocalizedField src ) {
		this.data = src.entrySet().stream()
			.map( entry -> {
				if( entry.getValue() == null ) { entry.setValue(""); }
				return entry;
			} )
			.collect( Collectors.collectingAndThen(
				Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ),
				Collections::unmodifiableMap
			) );
	}

	public LocalizedField( final LocalizedFieldDTO src ) {
		this.data = src.getData().entrySet().stream()
			.map( entry -> {
				if( entry.getValue() == null ) { entry.setValue(""); }
				return entry;
			} )
			.collect( Collectors.collectingAndThen(
				Collectors.toMap(
					entry -> Locale.forLanguageTag( entry.getKey() ),
					Map.Entry::getValue
				),
				Collections::unmodifiableMap
			) );
	}

	public String get( final Locale locale ) {
		return this.data.get( locale );
	}

	public Set<Map.Entry<Locale, String>> entrySet() {
		return this.data.entrySet();
	}

}

```

To display sorted providers in admin part of application I use transient property `sortableTitle` to avoid reading and parsing all the data just to provide a sort.

**Provider.java**

```auto
public class Provider {

	@Embedded
	@AttributeOverride(name = "data",
		column = @Column(name = "name", columnDefinition = "TEXT"))
	@FullTextField(projectable = Projectable.NO, searchable = Searchable.YES,
		analyzer = "customAnalyzer",
		valueBridge = @ValueBridgeRef(type = LocalizedFieldBridge.class))
	private LocalizedField name;

	@Override @Transient
	@IndexingDependency(derivedFrom = {
		@ObjectPath(@PropertyValue(propertyName = "name")),
		@ObjectPath({
			@PropertyValue(propertyName = "parent"),
			@PropertyValue(propertyName = "name")
		})
	})
	@KeywordField(name = "sortableTitle", projectable = Projectable.NO,
				searchable = Searchable.NO, sortable = Sortable.YES)
	public String getSortableTitle() {
		final String result = this.name.get(LocaleUtil.SORT_LOCALE);
		if (this.parent == null) {
			return TextUtil.getFoldedString(result);
		}
		// FIXME exception is thrown during reindex process
		if (this.parent.name == null) {
			throw new IllegalStateException(
				"Provider parent is NOT NULL, but its name IS NULL; " + 
				"child provider: " + this.name.get(LocaleUtil.SORT_LOCALE)
			);
		}
		final String parentName = this.parent.name.get(LocaleUtil.SORT_LOCALE);
		return TextUtil.getFoldedString(parentName + '.' + result);
	}

```

During indexing process `IllegalStateException` is thrown from my method `getSortableTitle()`. When I put a breakpoint on a row with throw statement, I can find parent `Provider` being initialized partially - its name is `null`. I guess the problem is that I use `@Converter` to populate `LocalizedField name` and this converter is perhaps not triggered during parent `Provider` initialization. It could be something else, but so far I don’t know. When using `SELECT` statement from my SQL tool, I can see both names are present - for child `Provider` and parent `Provider`.

Any ideas? Except putting all relations to eager because it would destroy any chance for my application to be fast and responsive.

---

<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:** [January 20, 2023, 7:27am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/2 "2023-01-20T07:27:15Z")

</div>

> [@horvoje](#):
>
> org.hibernate.AssertionFailure: force initializing collection loading

This looks like a bug in Hibernate ORM 6.1. If you can report it on [Jira](https://hibernate.atlassian.net/browse/HHH) with a reproducer, we will have a look. Maybe try with Hibernate ORM 6.2.0.CR1 first, in case this has already been fixed?

> [@horvoje](#):
>
> During indexing process `IllegalStateException` is thrown from my method `getSortableTitle()`. When I put a breakpoint on a row with throw statement, I can find parent `Provider` being initialized partially - its name is `null`.

I think your problem is there:

> [@horvoje](#):
>
> ` if (this.parent.name == null) {`

You are accessing a field directly, which sidesteps Hibernate proxies and lazy initialization. Use a getter instead and everything should be fine:

> [@horvoje](#):
>
> ` if (this.parent.getName() == null) {`

(use `getName()` in the code below too, of course).

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 20, 2023, 10:20am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/3 "2023-01-20T10:20:04Z")

</div>

> [@yrodiere](#):
>
> You are accessing a field directly, which sidesteps Hibernate proxies and lazy initialization. Use a getter instead and everything should be fine:

Thanks!  
If I were thinking, I would have to come to this on my own.  
Changed it to use getter and now this part is working as expected.  
During the day I’ll try to file a bug report for the other issue, but only if 6.2 shows same exceptions.

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 20, 2023, 10:45am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/4 "2023-01-20T10:45:32Z")

</div>

Tried to update dependencies to give a try 6.2.0.CR1, but no luck - app won’t start.  
Exception is thrown in early stage of boot attempt:

> java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified

Log is available [here](https://horvoje.net/share/hs6/boot-fail.log).

* * *

**CurrencyRepositoryImpl.java**

```auto
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

public class CurrencyRepositoryImpl implements CurrencyExtendRepository {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public CustomCurrency findByIso4217(final String iso4217code) {
		java.util.Currency jc = java.util.Currency.getInstance(iso4217code);
		final QCustomCurrency customCurrency = QCustomCurrency.customCurrency;
		return
			new JPAQueryFactory(this.entityManager)
				.select(customCurrency)
				.from(customCurrency)
				.where(customCurrency.javaCurrency.eq(jc))
				.fetchFirst();
	}

	// ... more methods
}

```

* * *

**pom.xml**

* * *

**parent**

```auto
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>3.0.1</version>
</parent>

```

**versions**

```auto
<properties>
	<java.version>18</java.version>
	<hibernate-core.version>6.2.0.CR1</hibernate-core.version>
	<org.hibernate.search.version>6.1.7.Final</org.hibernate.search.version>
</properties>

```

**Spring Boot starter**

```auto
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.hibernate.orm</groupId>
			<artifactId>hibernate-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>

```

**Hibernate core**

```auto
<dependency>
	<groupId>org.hibernate.orm</groupId>
	<artifactId>hibernate-core</artifactId>
	<version>${hibernate-core.version}</version>
</dependency>

```

**Hibernate Search**

```auto
<dependency>
	<groupId>org.hibernate.search</groupId>
	<artifactId>hibernate-search-mapper-orm-orm6</artifactId>
	<version>${org.hibernate.search.version}</version>
	<exclusions>
		<exclusion>
			<groupId>org.hibernate.orm</groupId>
			<artifactId>hibernate-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>

<dependency>
	<groupId>org.hibernate.search</groupId>
	<artifactId>hibernate-search-backend-lucene</artifactId>
	<version>${org.hibernate.search.version}</version>
</dependency>

```

---

<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:** [January 20, 2023, 10:50am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/5 "2023-01-20T10:50:31Z")

</div>

Looks like a Spring problem, probably unrelated. You’d better report your previous problem on JIRA then, even if you can only reproduce it on 6.1. Please include a (minimal) reproducer though (ZIP file containing the source code or link to a github repository). Ideally you’d use the templates available there: [hibernate-test-case-templates/search/hibernate-search-6 at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/tree/main/search/hibernate-search-6)

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 20, 2023, 10:57am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/6 "2023-01-20T10:57:32Z")

</div>

To create reproducer it would take days - perhaps the only way would be to copy the whole project to new one and then cut out class by class to minimize it.  
I’ll contact you with message if I find how to and suggest another way.

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 20, 2023, 5:18pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/7 "2023-01-20T17:18:40Z")

</div>

Just for test purposes I replaced all `fetch = FetchType.LAZY` with `fetch = FetchType.EAGER` (I have it on 29 places in project) and the exception is still the same:

> org.hibernate.AssertionFailure: force initializing collection loading

Now I’m thinking that could be endles loop because I have back references in entities. For example, `Article` has a collection of `PriceInfo` and each `PriceInfo` has back reference to `Article`.

Still trying to think of a plan how to create a minimal reproducer.

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 22, 2023, 2:22pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/8 "2023-01-22T14:22:28Z")

</div>

@yrodiere Can’t understand why, but when I forced all my relations to LAZY, indexing is finished successfully. Before I had 29 LAZY relations and 3 EAGER relations (user roles to user and two similar).

 ![xxx](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/d/d1243669a590ab0ff02867ef681dbdd3a8d22332.png)

As I switched to new Spring Boot and new Hibernate also, I have some issues with Thymeleaf and web mvc security which makes my app unusable, but here I’m showing an index from Luke which looks like somethint usable.

---

<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:** [January 22, 2023, 4:01pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/9 "2023-01-22T16:01:04Z")

</div>

Yes, this is most likely a bug in Hibernate ORM 6.1 not handling association loading correctly in your particular case. As is obvious from the [ORM 6 migration guide](https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#fetch-circularity-determination), a lot of things changed in ORM 6 and thus different bugs were to be expected.

If you want this fixed, you will need to identify the problematic association and provide a reproducer.

@beikov might have already fixed this in 6.2, but since you cannot test your application with 6.2 yet, that’s hard to say.

Reminder for @beikov , this is the error message. Rings a bell?

> [@horvoje](#):
>
> org.hibernate.AssertionFailure: force initializing collection loading

---

<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:** [January 23, 2023, 7:36am UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/10 "2023-01-23T07:36:35Z")

</div>

Split this topic on January 23, 2023, 7:36am UTC

A post was split to a new topic: [Numeric enum mapping](https://discourse.hibernate.org/t/numeric-enum-mapping/7179)

---

<div class="post-metadata">

**Author:** ![ls-urs-keller](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ls-urs-keller/32/2270_2.png) [@ls-urs-keller](https://discourse.hibernate.org/u/ls-urs-keller)\
**Post date:** [February 18, 2023, 4:25pm UTC](https://discourse.hibernate.org/t/force-initializing-collection-loading/7172/11 "2023-02-18T16:25:02Z")

</div>

I have filed a Jira here [[HHH-16199] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-16199), it includes a link to a reproducer project.
