[hibernate-dev] [ORM|OGM|Search] Adding an unwrap() new method to org.hibernate.Session

Sanne Grinovero sanne at hibernate.org
Tue May 19 06:44:23 EDT 2015

I was looking at how Search and OGM benefit from the
SessionDelegatorBaseImpl; extending this base class was a good
progress but there is something missing.

OGM has to still reimplement all methods defined by the EventSource
contract, making the implementation confusing as while it extends the
base delegate it still has to directly delegate for many other
So my initial impulse was to go and refactor the
SessionDelegatorBaseImpl to actually be an "EventSourceBaseDelegator",
adding the additional interface and delegates for the missing methods.

But questioning why the OGM Session needs to implement EventSource, it
turns out the reason is that when it uses Search this needs to access
the EventSource and will cast the given Session to one; so OGM's
Session has to implement that interface too, only for this reason.

Such a cast has never been dangerous for Search in the past as there
was only one Session but the existence of the SessionDelegatorBaseImpl
in ORM is making it easy to see more Session implementations which
actually don't implement EventSource (and often don't need to).
Rather than forcing / recommending all implementors to consistently
extend EventSource as well, it would be nice to introduce a way to get
a typesafe reference to the only EventSource we need.

My proposal is to add an unwrap() method to the public
org.hibernate.Session, like the one in JPA, and enhance the
implementation of both SessionImpl#unwrap and the
EntityManagerImpl#unwrap() to also satisfy requests for the
EventSource.class, and then possibly other such components as needed.

I would then be able to remove some casts in various places, and OGM's
Session wouldn't need to implement the EventSource interface.



More information about the hibernate-dev mailing list