-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Scoped EntityManager bean cannot get unwrapped to Session (with Hibernate 5.2) [SPR-15010] #19577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Juergen Hoeller commented As far as I can see, you'll get a proxy for your declared return type You could consider declaring a raw scoped object without a proxy mode here. As long as you're only injecting it into request-scoped application beans, this is going to work fine. For a JPA Finally, as of Hibernate 5.2, the |
Rodolfo Labsch commented "However, I'm wondering why an unwrap call wouldn't work since it should be delegated to the actual target EntityManager in this case... What's the stacktrace there?" if (retVal != null && retVal == target && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this" and the return type of the method
// is type-compatible. Note that we can't help if the target sets
// a reference to itself in another returned object.
retVal = proxy;
} "You could consider declaring a raw scoped object without a proxy mode here". "Finally, as of Hibernate 5.2, the Session interface extends EntityManager directly." |
Rodolfo Labsch commented This looks like a bug to me, although I am not certain this could be implemented with pure jdk proxies. Just my two cents here:
|
Juergen Hoeller commented This seems to be caused by Ironically, this is probably caused exactly by Hibernate 5.2's merging of the |
Rodolfo Labsch commented Thank you! |
Juergen Hoeller commented In terms of proxy creation time, there is not much we can do beyond introspecting the declared return type of the factory method. Technically, we have to create and inject this shared proxy before we ever call the factory method for any particular request-scoped instance. We can't amend a proxy's implemented interfaces after creation time - not with JDK proxies and not with CGLIB either -, so if you keep your return type to the generic As a consequence, for a setup scenario like yours, we'll have to make As an alternative to a custom request-scoped |
Juergen Hoeller commented I've revised our proxy replacement mechanism to not apply for generic return type signatures or plain |
Rodolfo Labsch opened SPR-15010 and commented
Cannot cast spring created proxy to a different object interface.
e.g.:
Given the bean:
@Bean
@Scope
(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)public EntityManager myEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
If I try to cast it or maybe unwrap the instance, we get a class cast exception:
@Autowired
EntityManager em;
Session s = em.unwrap(Session.class) -> ClassCastException
Session s = ((Session) em) -> ClassCastException
Affects: 4.2.8
Issue Links:
Referenced from: commits 6d1cae2, d15df34
The text was updated successfully, but these errors were encountered: