0 Replies Latest reply on Jul 1, 2013 4:17 PM by rdiddly

    An object is not of the right class after retrieval

    rdiddly

      Moving from 4.2.2 to 7.1.1, using Hibernate in both cases, I have a strange situation. In 4.2.2 I packaged the model classes (entities, but really POJOs mapped using hbm.xml files) in the same jar as the hbm.xml files. In the same jar, I packaged the Stateless Session Beans that provided methods to retrieve instances from the database and return them to the client.

       

      Consider this method in ContentManagerBean:

       


      public Banner getTopNavBarBanner() {

      Banner answer = null;

      try {

      Session hbmSession = HomeFactory.getInstance().lookupHibernateSessionFactory().getCurrentSession();

      answer = (Banner) hbmSession.getNamedQuery(Banner.NamedQuery.GetTopNavBarBanner.getQueryName())

      .setMaxResults(1)

      .uniqueResult();

      } catch (Exception e) {

      log.error("Failed getting content for topNavBar banner.", e);

      }

      return answer;

      }

       

      This code worked fine. It returned an instance of com.app.hbm.model.Banner.

       

      This is the version as rewritten for JBoss 7.1.1:

       

      @PersistenceContext(unitName="app")

      private EntityManager em;

       

      public Banner getTopNavBarBanner() {

      Banner answer = null;

      try {

      Session hbmSession = (Session) em.getDelegate();

      answer = (Banner) hbmSession.getNamedQuery(Banner.NamedQuery.GetTopNavBarBanner.getQueryName())

      .setMaxResults(1)

      .uniqueResult();

      } catch (Exception e) {

      log.error("Failed getting content for topNavBar banner.", e);

      }

      return answer;

      }

       

      In this version, I get a class cast exception casting the return value from the query (uniqueResult) to an instance of Banner. I can see in the debugger that the value returned by the query is in fact an instance of com.app.hbm.model.Banner, but when I try to cast it to that same class I get a class cast exception.

       

      I can only conclude that these are two different classes as seen by the code executing the query and the code attempting to cast the return value, and so it must be a classloader issue, but I can't figure out what I need to do to fix it. For 7.1.1 I moved the hbm.xml files to a separate jar which also contains the persistence.xml file. This jar gets placed in the ear's lib folder. (I did this in order to get Hibernate to scan the hbm.xml files for persistent classes.) The classes that represent the model, i.e., are referenced by the hbm.xml files, are in the jar where they were originally defined, the same jar as contains the Stateless Session Bean whose method appears above.

       

      I'm not sure this is an appropriate question for the JBoss forums, I've posted it on Hibernate as well. I hope someone can help me to understand what's going on.

       

      Thanks,

      Richard