4 Replies Latest reply on Oct 9, 2008 6:54 AM by Ilavenil Arumugam

    Acces Configuration/Persistent Class at run time for read on

    Ilavenil Arumugam Newbie


      I want to access the org.hibernate.cfg.Configuration Object after all the configuration is done at some point in my application. This is just for read only purposes. Or even if i can retrieve the list of PersistentClass, then it will be very useful. Is there a way to do it? Please help

      A bit of background on what I am doing: I have a typical application with some entities / session bean and client application. I am implementing entity versioning as a cross cutting behaviour. This means I need to know the current entity configuration to constuct the versioning entities configuration when I turn on versioning. Hope this helps!


        • 1. Re: Acces Configuration/Persistent Class at run time for rea
          Matt C Newbie

          Hi Ilavenil,

          If you only have one Persistence Unit, you can get a list of entity classes this way:

          // Get all entity classes
          org.hibernate.SessionFactory sf = ((org.jboss.ejb3.entity.HibernateSession) em).getHibernateSession().getSessionFactory();
          Map<?,?> allClassMetadata = sf.getAllClassMetadata();
          for (Object value: allClassMetadata.values()) {
           EntityPersister ep = (EntityPersister) value;
           Class<?> entityClass = ep.getClassMetadata().getMappedClass(EntityMode.POJO);
           // Do something with the class.....

          If you have more than one persistence unit, you could use
          @PersistenceContext(unitName="") or similar annotations to inject an EntityManager or SessionFactory for each.

          If you can't do this or don't know the persistence units at compile-time, there is another way I know of to get all Persistence Units, which involves editing persistence.xml to register hibernate SessionFactory objects in JNDI and scanning for them.


          • 2. Re: Acces Configuration/Persistent Class at run time for rea
            Ilavenil Arumugam Newbie

            Thanks Matt for your reply. I do not want to access the underlying entity class but the PersistentClass (org.hibernate.mapping.PersistentClass) created by hibernate when it loads the persistence unit.

            The HbmBinder creates RootClass ( org.hibernate.mapping.RootClass) which extends or.hibernate.mapping.PersistentClass when hibernate loads the persistence unit. This can easily be accessed from the configuration object using configuration.getClassMappings(). Hence the need to access configuration object at runtime. Hope it makes sense!

            Why I need this: I need access to this so that I can pass this to Envers (another Jboss project) to create mapping for my versioned entities.

            At the moment, I intercept the loading of persistence unit and put these classes in a cache to access it at a later point of time. This is kind of workaround. But I was expecting hibernate to give me access to the classes (atleast for read-only purposes). Please let me know if you have a better way to implement this. Thanks!

            • 3. Re: Acces Configuration/Persistent Class at run time for rea
              Matt C Newbie

              In a JBoss EJB3 environment I've never found a way to access the hibernate Configuration object or the metadata it contains e.g. PersistentClass.

              To my knowledge, this is available at startup time only, and JBoss doesn't provide any access to it.

              After initialization (at runtime), JBoss/Hibernate seems to provide another type of object for class metadata that implements the interfaces ClassMetadata and EntityPersister instead.

              The constructor for this object takes PersistentClass as input internally.

              AbstractEntityPersister(PersistentClass persistentClass, CacheConcurrencyStrategy cache, SessionFactoryImplementor factory)

              I did read in a JIRA bug report somebody requested access to the Configuration object from a Web app and the bug was resolved after some coding; but I never figured out how to apply this to a normal EJB3 app.


              • 4. Re: Acces Configuration/Persistent Class at run time for rea
                Ilavenil Arumugam Newbie

                Thanks Matt! As I said, in my interceptor if I implement Initializable interface, I get access to the Configuration object on persistence unit deployment. From which I cache the PersistentClass and use it at a later point in my application..