Here is what happens.
Phase 1: Initialization of the entity proxy
In our failing test (1), we load the ConcreteEntity#1 via the lazy property of LazyAbstractEntityReference. You'll notice that the property of LazyAbstractEntityReference is of type AbstractEntity, not ConcreteEntity.
It creates a proxy for ConcreteEntity#1 in the session but the proxy is of type AbstractEntity.
This is done entirely outside our search and is not related to it. It's just that this entity was loaded before.
Phase 2: let's search this entity
When we executes the search, ObjectLoaderHelper.returnAlreadyLoadedObjectsInCorrectOrder (2) is called. This part of the code supposes that each entity received is already initialized. Which is the case... except in this situation.
When we call session.load() in ObjectLoaderHelper.executeLoad (3), ORM detects that the proxy is not right and decides to narrow it: we want a ConcreteEntity proxy, not the AbstractEntity proxy previously loaded.
If you take a look at StatefulPersistenceContext.narrowProxy (4), in our case, it creates a new proxy with the ConcreteEntity class. The problem is that this new proxy is not initialized, unlike our previously loaded proxy.
How to solve this issue
In Search
I'm wondering if using session.get in ObjectLoaderHelper.executeLoad (3) wouldn't be acceptable. Most of the entities should be loaded and already initialized so it shouldn't be a performance problem?
In ORM
I think ORM should initialize the new proxy created if the original proxy was already initialized. Apart from this very issue in Search, it's also a performance problem.
Something along the lines of https://gist.github.com/gsmet/9663149 might work (but has not been tested yet).
References
(1) https://github.com/openwide-java/hibernate-search/commit/f967e2b4e396fc2e35e7d3ed6fb4f18b00589a12 (2) https://github.com/hibernate/hibernate-search/blob/master/orm/src/main/java/org/hibernate/search/query/hibernate/impl/ObjectLoaderHelper.java#L73 (3) https://github.com/hibernate/hibernate-search/blob/master/orm/src/main/java/org/hibernate/search/query/hibernate/impl/ObjectLoaderHelper.java#L101 (4) https://github.com/hibernate/hibernate-orm/blob/4.3/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L638
– Guillaume
|