See https://forum.hibernate.org/viewtopic.php?f=1&t=1004366
Here is a unit test failed case:
Set hibernate.cfg.xml with thread session context
You will get this stack trace
As described in forum, this problem occurred because the session object we get using sessionFactory.getCurrentSession(); is an wrapped ThreadLocalSessionContext instance that can not be cast into LobCreationContext. There is no easy way to unwrap and get the original session object, to pass to createBlob method.
I believe this would also affect sessions obtained from ManagedSessionContext.currentSession() and JTASessionContext.currentSession(), which return org.hibernate.classic.Session, because that does not extend LobCreationContext either.
In 3.6, session.getLobHelper().createBlob( bytes ), should be used instead.
Zemian, could you give 3.6.0 a try?
Thanks,
Gail
I don't think that is really the reason. I think it is because ThreadLocalSessionContext wraps the "real session" in a proxy but does not "layer" LobCreationContext into that proxy definition. I'd be shocked if this does not work from ManagedSessionContext and JTASessionContext
Hi there,
Yes the 3.6 version with "session.getLobHelper().createBlob( bytes )" does works. Thanks.
I have two additional feedbacks though:
1) In 3.6, even Hibernate.createBlob(blobBytes, session); is marked as deprecated, but if used, it still produces the same exception mentioned here. So in this case, it's not just deprecated, but it's broken. (Granted that it only for Thread context scenerio, but still a bit annoying.).
2) In 3.6, the javadoc on Hibernate.createBlob(blobBytes, session) says: "@deprecated Use {@link LobHelper#createBlob(byte[])} instead." Now this is not very good hint since LobHelper is an interface and the only implementation is a private inner class. It would be more helpful to let user know that they need to get this impl instance by session instance like "session.getLobHelper().createBlob( bytes )".
Hope this helps,
Zemian Deng
Bulk closing stale resolved issues