Proxy narrowing loses the initialized state of the original proxy

Description

Hi,

This is a followup of a Search bug: see https://hibernate.atlassian.net/browse/HSEARCH-1448?focusedCommentId=59117&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-59117 .

It is related to StatefulPersistenceContext.narrowProxy ( https://github.com/hibernate/hibernate-orm/blob/4.3/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L638 )

This issue leads to missing results in Search (which filters out uninitialized proxy) but IMHO it's also suboptimal for ORM as, after narrowing of a proxy, the new proxy has to be initialized again.

AFAICS, we can initialize the new proxy with the entity of the original one.

I have prepared a pull request with a test case and a fix along those lines.

Would be nice to have some feedback about it and especially to be sure that it's safe.

Thanks.

Environment

None

Status

Assignee

Guillaume Smet

Reporter

Guillaume Smet

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.2.11
4.3.4

Priority

Major