-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: EntityManager.getDelegate()
PostPosted: Tue Nov 06, 2007 11:05 am 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi,

I have had problems to do this code work.

Code:
public class GenericEAO implements IGenericEAO {
    @PersistenceContext(unitName="Hibernate")
    protected EntityManager em;
    ...
    ...
    ...
}

@Stateless
public class MercadoriaEAO extends GenericEAO implements IMercadoriaEAO {
    public List<Mercadoria> findByExample(Mercadoria mercadoria, String[] colecoes) {
        try {
            Session hibernateSession = (Session)em.getDelegate();
            Criteria criteria = hibernateSession.createCriteria(Mercadoria.class);
            criteria.add(Example.create(mercadoria).enableLike(MatchMode.ANYWHERE));
            if(colecoes != null) {
                for(String col: colecoes) {
                    criteria.setFetchMode(col,FetchMode.JOIN);
                }
            }
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            List<Mercadoria> mercadorias = (List<Mercadoria>) criteria.list();
            hibernateSession.close();
            return mercadorias;
        } catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    ...
    ...
    ...
}


It's returning me the following exception:

Quote:
java.lang.ClassCastException: org.hibernate.ejb.EntityManagerImpl


So, Anyone could help me?

Thanks,
Cleiton


Last edited by ccllss on Tue Nov 06, 2007 2:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 1:44 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Which app server are you running on?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 2:00 pm 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi Emmanuel,

I'm using Glassfish.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 2:32 pm 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I have run into this problem before when running in JBoss AS. Try getting the delegate of the EntityManager.getDelegate() method, it worked for me in JBoss.

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 2:47 pm 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
andydale wrote:
Hi,

I have run into this problem before when running in JBoss AS. Try getting the delegate of the EntityManager.getDelegate() method, it worked for me in JBoss.

Cheers,

Andy


Andy,

So, the problem is in this line:

Code:
Session hibernateSession = (Session) em.getDelegate();


This casting error happens because Glassfish inject in EntityManager interface (em variavel) this class com.sun.enterprise.util.EntityManagerWrapper. This way when I call getDelegate() method, it returns me a org.hibernate.ejb.EntityManagerImpl class which doesn't mach with Session.


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 3:46 pm 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I think that line is the cause, i had the exact same exception in JBoss (4.0.4) with an injected EntityManager. In the end i had to use the following call to avoid the same class cast execption as you have/had:

Code:
(Session)((EntityManagerImpl)em.getDelegate()).getDelegate();


This might (not certain though) not be the case with an application managed entity manger (obtained from an EntityManagerFactory)

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 9:27 am 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi Andy,

That's great! It's working now.

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 6:44 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Glassfish has the same bug JBoss As had

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.