PersistenceContext and Hibernate session

View: New views
10 Messages — Rating Filter:   Alert me  

PersistenceContext and Hibernate session

by chawax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

In my application, I have two persistence contexts for the same persistence unit, one for JPA, the other for Hibernate :

    /**
     * Inject persistence context t4Seam    
     */
    @javax.persistence.PersistenceContext(unitName = "t4Seam")    
    protected javax.persistence.EntityManager emanager;

    /**
     * Inject Hibernate Session
     */
    @javax.persistence.PersistenceContext(unitName = "t4Seam")    
    protected org.hibernate.Session hibernateSession;

It used to work with JBoss. But with OpenEJB, I have the following error:
"Use of @PersistenceContext only applies to EntityManager references"

Is it intented or is it a bug ?

Olivier

Re: PersistenceContext and Hibernate session

by Martin Vysny-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 2008-07-11 at 06:50 -0700, chawax wrote:

> Hi,
>
> In my application, I have two persistence contexts for the same persistence
> unit, one for JPA, the other for Hibernate :
>
>     /**
>      * Inject persistence context t4Seam    
>      */
>     @javax.persistence.PersistenceContext(unitName = "t4Seam")    
>     protected javax.persistence.EntityManager emanager;
>
>     /**
>      * Inject Hibernate Session
>      */
>     @javax.persistence.PersistenceContext(unitName = "t4Seam")    
>     protected org.hibernate.Session hibernateSession;
>
> It used to work with JBoss. But with OpenEJB, I have the following error:
> "Use of @PersistenceContext only applies to EntityManager references"
>
> Is it intented or is it a bug ?
>
> Olivier
I think it's intended: Hibernate API is not part of JPA/EJB3
specification afaik.

Martin


signature.asc (196 bytes) Download Attachment

Re: PersistenceContext and Hibernate session

by chawax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, Hibernate is not part of JPA (even though JPA is based on concepts created by Hibernate). But it can be used as a JPA implementation, then it adds features to JPA ones (criterias for example).
I think my problem is that org.hibernate.Session does not implement javax.persistence.EntityManager, so it looks like OpenEJB is right when it throws an error about this ;)

I found there's a org.hibernate.ejb.HibernateEntityManager that implements javax.persistence.EntityManager, I think I should use this one instead of org.hibernate.Session. I'm going to test this.

Martin Vysny-2 wrote:
On Fri, 2008-07-11 at 06:50 -0700, chawax wrote:
> Hi,
>
> In my application, I have two persistence contexts for the same persistence
> unit, one for JPA, the other for Hibernate :
>
>     /**
>      * Inject persistence context t4Seam    
>      */
>     @javax.persistence.PersistenceContext(unitName = "t4Seam")    
>     protected javax.persistence.EntityManager emanager;
>
>     /**
>      * Inject Hibernate Session
>      */
>     @javax.persistence.PersistenceContext(unitName = "t4Seam")    
>     protected org.hibernate.Session hibernateSession;
>
> It used to work with JBoss. But with OpenEJB, I have the following error:
> "Use of @PersistenceContext only applies to EntityManager references"
>
> Is it intented or is it a bug ?
>
> Olivier

I think it's intended: Hibernate API is not part of JPA/EJB3
specification afaik.

Martin

 

Re: PersistenceContext and Hibernate session

by chawax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I tried this :

    protected org.hibernate.ejb.HibernateEntityManager emanager;

    @javax.persistence.PersistenceContext(unitName = "t4Seam")    
    public void setEntityManager(javax.persistence.EntityManager entityManager) {
    this.emanager = (org.hibernate.ejb.HibernateEntityManager) entityManager;
    }

But I have a ClassCastException :

    java.lang.ClassCastException: org.apache.openejb.persistence.JtaEntityManager

Is there a place where I could configure the entity manager to use Hibernate one instead of OpenEJB one ?

Re: PersistenceContext and Hibernate session

by Dain Sundstrom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

JtaEntityManager is a wrapper around the raw EntityManager instance we  
get from the JPA provider.  JtaEntityManager performs the task  
required by the EJB spec such as committing the flushing the JPA data  
at the end of the transaction and assuring that all access to the same  
persistence unit in a transaction uses the same raw EntityManager.  
You can get access to the raw underlying EntityManager by calling  
getDelegate(), but be careful.  You should not cache the results of  
getDelegate() as the raw EntityManager instance will change for each  
transaction.  You'll want to do something like this:

     @javax.persistence.PersistenceContext(unitName = "t4Seam")
     protected javax.persistence.EntityManager emanager;

     public HibernateEntityManager getHibernateSession() {
         return (HibernateEntityManager) emanager.getDelegate();
     }

     public void doIt() {
         getHibernateSession().doSomething();
     }

-dain

On Jul 11, 2008, at 8:09 AM, chawax wrote:

>
> I tried this :
>
>    protected org.hibernate.ejb.HibernateEntityManager emanager;
>
>    @javax.persistence.PersistenceContext(unitName = "t4Seam")
>    public void setEntityManager(javax.persistence.EntityManager
> entityManager) {
>     this.emanager = (org.hibernate.ejb.HibernateEntityManager)
> entityManager;
>    }
>
> But I have a ClassCastException :
>
>    java.lang.ClassCastException:
> org.apache.openejb.persistence.JtaEntityManager
>
> Is there a place where I could configure the entity manager to use  
> Hibernate
> one instead of OpenEJB one ?
> --
> View this message in context: http://www.nabble.com/PersistenceContext-and-Hibernate-session-tp18404318p18406052.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>


Re: PersistenceContext and Hibernate session

by David Blevins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Olivier,

This document may help you understand how things work from a high level.

   http://cwiki.apache.org/OPENEJBx30/jpa-concepts.html

The section of bullet points for "With <persistence-unit transaction-
type="TRANSACTION">" has a good description of what's going on.  When  
reading it, in your mind substitute every occurrence of  
"PersistenceContext/Cache" with "HibernateEntityManager instance".

-David

On Jul 11, 2008, at 8:55 AM, Dain Sundstrom wrote:

> JtaEntityManager is a wrapper around the raw EntityManager instance  
> we get from the JPA provider.  JtaEntityManager performs the task  
> required by the EJB spec such as committing the flushing the JPA  
> data at the end of the transaction and assuring that all access to  
> the same persistence unit in a transaction uses the same raw  
> EntityManager.  You can get access to the raw underlying  
> EntityManager by calling getDelegate(), but be careful.  You should  
> not cache the results of getDelegate() as the raw EntityManager  
> instance will change for each transaction.  You'll want to do  
> something like this:
>
>    @javax.persistence.PersistenceContext(unitName = "t4Seam")
>    protected javax.persistence.EntityManager emanager;
>
>    public HibernateEntityManager getHibernateSession() {
>        return (HibernateEntityManager) emanager.getDelegate();
>    }
>
>    public void doIt() {
>        getHibernateSession().doSomething();
>    }
>
> -dain
>
> On Jul 11, 2008, at 8:09 AM, chawax wrote:
>
>>
>> I tried this :
>>
>>   protected org.hibernate.ejb.HibernateEntityManager emanager;
>>
>>   @javax.persistence.PersistenceContext(unitName = "t4Seam")
>>   public void setEntityManager(javax.persistence.EntityManager
>> entityManager) {
>>   this.emanager = (org.hibernate.ejb.HibernateEntityManager)
>> entityManager;
>>   }
>>
>> But I have a ClassCastException :
>>
>>   java.lang.ClassCastException:
>> org.apache.openejb.persistence.JtaEntityManager
>>
>> Is there a place where I could configure the entity manager to use  
>> Hibernate
>> one instead of OpenEJB one ?
>> --
>> View this message in context: http://www.nabble.com/PersistenceContext-and-Hibernate-session-tp18404318p18406052.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>
>


Re: PersistenceContext and Hibernate session

by chawax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Dain, your suggestion worked perfect with OpenEJB.

But it doesn't work with JBoss AS 4.2.x. I have this error :
java.lang.ClassCastException: org.hibernate.impl.SessionImpl

Anyway, that's not an Open EJB issue, so I will ask on Hibernate or JBoss forums.

Thanks.

-Olivier

Dain Sundstrom wrote:
You'll want to do something like this:

     @javax.persistence.PersistenceContext(unitName = "t4Seam")
     protected javax.persistence.EntityManager emanager;

     public HibernateEntityManager getHibernateSession() {
         return (HibernateEntityManager) emanager.getDelegate();
     }

     public void doIt() {
         getHibernateSession().doSomething();
     }

-dain

Re: PersistenceContext and Hibernate session

by chawax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I finally could make it work with JBoss AS ... but it doesn't work the same as Open EJB !
On JBoss AS the getDelegate method returns Hibernate session, while it returns Hibernate entity manager on Open EJB. So to make it work, I made this :

    protected javax.persistence.EntityManager emanager;

    protected org.hibernate.Session hibernateSession;

    @javax.persistence.PersistenceContext(unitName = "t4Seam")
    public void setEntityManager(javax.persistence.EntityManager entityManager) {
    this.emanager = entityManager;
    if (entityManager.getDelegate() instanceof org.hibernate.ejb.HibernateEntityManager) {
    this.hibernateSession = ((org.hibernate.ejb.HibernateEntityManager) entityManager.getDelegate()).getSession();
    }
    else {
    this.hibernateSession = (org.hibernate.Session) entityManager.getDelegate();
    }
    }

So I have a question : which one is right with this, Open EJB or JBoss AS ? Or maybe the JPA specifications are not clear about this, so there can be different implementations for this method ?

-Olivier



Thanks Dain, your suggestion worked perfect with OpenEJB.

But it doesn't work with JBoss AS 4.2.x. I have this error :
java.lang.ClassCastException: org.hibernate.impl.SessionImpl

Anyway, that's not an Open EJB issue, so I will ask on Hibernate or JBoss forums.

Thanks.

-Olivier

Dain Sundstrom wrote:
You'll want to do something like this:

     @javax.persistence.PersistenceContext(unitName = "t4Seam")
     protected javax.persistence.EntityManager emanager;

     public HibernateEntityManager getHibernateSession() {
         return (HibernateEntityManager) emanager.getDelegate();
     }

     public void doIt() {
         getHibernateSession().doSomething();
     }

-dain


Re: PersistenceContext and Hibernate session

by David Blevins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Jul 15, 2008, at 6:11 AM, chawax wrote:

>
> I finally could make it work with JBoss AS ... but it doesn't work  
> the same
> as Open EJB !
> On JBoss AS the getDelegate method returns Hibernate session, while it
> returns Hibernate entity manager on Open EJB.

Congrats, glad you got it to work in both platforms!

> So to make it work, I made
> this :
>
>    protected javax.persistence.EntityManager emanager;
>
>    protected org.hibernate.Session hibernateSession;
>
>    @javax.persistence.PersistenceContext(unitName = "t4Seam")
>    public void setEntityManager(javax.persistence.EntityManager
> entityManager) {
>     this.emanager = entityManager;
>     if (entityManager.getDelegate() instanceof
> org.hibernate.ejb.HibernateEntityManager) {
>     this.hibernateSession =  
> ((org.hibernate.ejb.HibernateEntityManager)
> entityManager.getDelegate()).getSession();
>     }
>     else {
>     this.hibernateSession = (org.hibernate.Session)
> entityManager.getDelegate();
>     }
>    }
>
> So I have a question : which one is right with this, Open EJB or  
> JBoss AS ?
> Or maybe the JPA specifications are not clear about this, so there  
> can be
> different implementations for this method ?

The spec simply says "The result of this method is implementation  
specific."  So either version is valid strictly speaking.

On that note, we could add a flag that would have our getDelegate()  
detect if it's hibernate and return the hibernate session if that  
would make your life easier.

-David


Re: PersistenceContext and Hibernate session

by Dain Sundstrom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jul 15, 2008, at 2:32 PM, David Blevins wrote:

> On Jul 15, 2008, at 6:11 AM, chawax wrote:
>
>>
>> I finally could make it work with JBoss AS ... but it doesn't work  
>> the same
>> as Open EJB !
>> On JBoss AS the getDelegate method returns Hibernate session, while  
>> it
>> returns Hibernate entity manager on Open EJB.
>
> Congrats, glad you got it to work in both platforms!
>
>> So to make it work, I made
>> this :
>>
>>   protected javax.persistence.EntityManager emanager;
>>
>>   protected org.hibernate.Session hibernateSession;
>>
>>   @javax.persistence.PersistenceContext(unitName = "t4Seam")
>>   public void setEntityManager(javax.persistence.EntityManager
>> entityManager) {
>>   this.emanager = entityManager;
>>   if (entityManager.getDelegate() instanceof
>> org.hibernate.ejb.HibernateEntityManager) {
>>   this.hibernateSession =  
>> ((org.hibernate.ejb.HibernateEntityManager)
>> entityManager.getDelegate()).getSession();
>>   }
>>   else {
>>   this.hibernateSession = (org.hibernate.Session)
>> entityManager.getDelegate();
>>   }
>>   }
>>
>> So I have a question : which one is right with this, Open EJB or  
>> JBoss AS ?
>> Or maybe the JPA specifications are not clear about this, so there  
>> can be
>> different implementations for this method ?
>
> The spec simply says "The result of this method is implementation  
> specific."  So either version is valid strictly speaking.
>
> On that note, we could add a flag that would have our getDelegate()  
> detect if it's hibernate and return the hibernate session if that  
> would make your life easier.

It would be cool if we could just support org.hibernate.Session as a  
recognized injection type.  Then anyone using code similar to the  
original code posted would just work.

-dain