6 Replies Latest reply on Sep 19, 2011 10:27 AM by angelocavalier

    We are getting the Hibernate Exception

    vacajboss

      We are getting the following Excpetion while dploying our application. We are using the hibernate. Migrating from Jboss 434 to Jboss 5.0.1.

       

      org.jboss.jpa.tx.TransactionScopedEntityManager cannot be cast to org.hibernate.ejb.HibernateEntityManager

       

      Please let us know what we need to do? Your response is highly appriciated.

        • 1. We are getting the Hibernate Exception
          wdfink

          Sound that you use direct the implementation for the EntityManager instead of interfaces and the implementation changed.

          Could you provide more information and a pice of code?

          • 2. We are getting the Hibernate Exception
            vandu620

            This exception happens when we are using findEntityById() method which has the following code

             

            org.hibernate.ejb.HibernateEntityManager hbEm= ((org.hibernate.ejb.HibernateEntityManager)em);

             

            We are casting the entity manager to the HibernateEntityManager.

             

            Is there any workaround for this casting problem exception?

            • 3. We are getting the Hibernate Exception
              wdfink

              Why do you cast the EM to a special implementation?

              Where the 'em' is from.

              Post a bit more of the relevant code that it is understandable what you try to do.

              • 4. Re: We are getting the Hibernate Exception
                vandu620

                I am really not sure about the special implementation

                The em is from  javax.persistence.EntityManager and we are casting to the specific Hibernate Entity Manager and below is the code in our methods...

                 

                This casting was working perfectly fine on Jboss 4.3.4GA but failing on Jboss5.0.1

                 

                public T findEntityById(T exampleInstance, long entityId) throws  EJBServiceException {

                T entityT;

                org.hibernate.ejb.HibernateEntityManager hbmEm = ((org.hibernate.ejb.HibernateEntityManager)em); 

                   entityT = (T)hbmEm.find(exampleInstance.getClass(), entityId);

                      return entityT;    

                }

                 

                @SuppressWarnings("unchecked")    

                public List findByCriteria(Criterion... criterions) throws  EJBServiceException {        

                org.hibernate.Criteria crit = ((org.hibernate.ejb.HibernateEntityManager)em) .getSession()   .createCriteria(getEntityBeanType());

                 

                                for (Criterion criterion : criterions) {

                                               crit.add(criterion);        

                                   }        

                return crit.list();    

                }

                • 5. Re: We are getting the Hibernate Exception
                  jaikiran

                  Why are you casting it to org.hibernate.ejb.HibernateEntityManager? Just cast it to javax.persistence.EntityManager

                  • 6. Re: We are getting the Hibernate Exception
                    angelocavalier

                    Hi VM,

                     

                    JBoss 5.0.1 use TransactionScopedEntityManager, cast yous EntityManager to TransactionScopedEntityManager and use method "getHibernateSession" to use hibernate functionalities.

                     

                    Some like this:

                     

                    public List findByCriteria(Criterion... criterions) throws  EJBServiceException {        

                    org.hibernate.Criteria crit = ( (org.jboss.jpa.tx.TransactionScopedEntityManager) em) .getHibernateSession()   .createCriteria(getEntityBeanType());

                     

                                    for (Criterion criterion : criterions) {

                                                   crit.add(criterion);        

                                       }        

                    return crit.list();

                    }