So after a little bit of investigation this is what it's all about.
The behaviour only occurs when you are in SE environment + you have to use any scope which triggers client proxy creation. If you simply use @Dependent, you are good to go.
The reason why we see this exception is the check in ProxyMethodHandler which decides to return self (meaning proxy) if it were to return a contextual instance which is equal and assignable to the the proxy. Whether it comes to that depends on whether Hibernate (or other provider) uses a wrapper or not. In SE, there is no wrapper, therefore it blows up. In EE there is one and so it works. Although in case of Hibernate it seems it isn't a mere wrapper though ( you could probably ask Hibernate guys about this).
In SE, the EM producer was giving me SessionImpl (not 'wrapped') while in EE I got EntityManagerImpl ('wrapped'). From spec point of view this is completely undefined. From Weld PoV, we do not want to change this check as it influences all proxies and was (I suppose) introduced on purpose.
Therefore, since there is a workaround of creating a wrapper (as you mentioned), I am closing this issue. Should you view it differently, feel free to reopen it and discuss it.
|