[hibernate-dev] unwrap (was Re: Hibernate Search 3.4 release train)

Emmanuel Bernard emmanuel at hibernate.org
Thu Feb 17 03:52:00 EST 2011

On Feb 16, 2011, at 6:21 PM, Scott Marlow wrote:

> Its a minor change but I would like to make a small enhancement to 
> EntityManager.unwrap().  I would like to support SessionFactory in case 
> I want to use it later in AS7...

I guess the alternatives are:
 - entityManager.unwrap(SessionFactory.class) //not implemented
 - entityManager.unwrap(Session.class).getSessionFactory()
 - ( (HibernateEntityManagerFactory) entityManager.getEntityManagerFactory() )

As you know, I don't feel like an EM should spit *Factories via the unwrap method. So for me option #2 is good enough.

> Is this completely silly or just mildly silly?  If creating a container 
> managed entity manager doesn't consume a database connection.  I propose 
> that its just mildly silly.  If its just mildly silly, I might want to 
> use the unwrap functionality to keep the AS injection code generic 
> (covering injecting a PU into a Hibernate SessionFactory).  Of course, 
> the other silly part is the idea that I would get an EM from the PU 
> (could lead to unexpected errors if something goes wrong).

I am a bit uncertain on what you want to achieve.
Do you want to create a EM that you will not use just to get the SessionFactory?
I'm not sure I like that very much and I would use option #3 to get the SessionFactory from the EMF.
Yes the code requires a cast but you depend on Hibernate classes anyway when you need a SessionFactory.

> It might be nice to have an unwrap method on the EntityManagerFactory 
> for this purpose.  Is that worth proposing for a future JPA release?

Yep, it's going to be added.

More information about the hibernate-dev mailing list