Indexing lazy collection


#1

Should I be able to index objects in a lazy collection?

DocumentBuilderIndexedEntity is pulling the un-initialized collection and running

Collection<?> collection = objectInitializer.initializeCollection( (Collection<?>) value );

I’m assuming this is where the lazy loaded collection should actually be pulled. The objectInitializer is hardcoded by SearchConfigurationFromHibernateCore to be HibernateStatelessInitializer.

The HibernateStatelessInitializer then passes the un-initialized back.

@Override
public <T> Collection<T> initializeCollection(Collection<T> value) {
	// supports pass-through only, would need HibernateSessionLoadingInitializer
	return value;
}

Is something misconfigured? Should I be able to use a HibernateSessionLoadingInitializer?


#2

Before we go into details, maybe you could explain to us the actual problem you’re experiencing: what is your configuration, your entity mapping, what code are you executing, what happens, what would you expect to happen instead?

Without this information, I’ll try to guess… As far as I can tell, HibernateStatelessInitializer is used wherever we expect every collection to be attached to a session, which means the collection should be able to initialize itself lazily on first access. If that initialization throws a LazyInitializationException, it means some of the content you are trying to index references detached entities/collections, and the most likely explanation for that is you did a session.clear() without doing a fullTextSession.flushToIndexes() first. See this for an explanation. If you don’t clear the session, then this might explain the issue.


#3

No error. It just thinks the collection is empty.

I’ll try to build out a test case off of https://github.com/hibernate/hibernate-test-case-templates/tree/master/search/hibernate-search-lucene

It look like the reflection call to get the collection is not triggering the code added by hibernate-enhance-maven-plugin to get the hibernate collection wrapper versus a blank collection.

At a break point in DocumentBuilderIndexedEntity when it thinks the collection is empty I can run a debug expression ((DomainObjectType)unproxiedInstance).getLazyCollecion() and do get the values.


#4

Thanks, a test case would be great. Be sure to set it up with the exact versions of Hibernate Search and Hibernate ORM you’re using, because I suspect this might be a bug in Hibernate ORM bytecode enhancement, and if I remember correctly, several of those bugs were fixed in recent versions of ORM.