Hibernate.createBlob() fails when used in current_session_context_class=thread mode

Description

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.

Environment

None

Activity

Show:
Gail Badner
December 23, 2010, 11:37 PM

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

Steve Ebersole
January 5, 2011, 5:54 PM

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

Zemian Deng
January 6, 2011, 6:03 AM

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

Steve Ebersole
March 21, 2011, 7:09 PM

Bulk closing stale resolved issues

Fixed

Assignee

Steve Ebersole

Reporter

Zemian Deng

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Major
Configure