0

In Hibernate 3, in the org.hibernate.cfg.Configuration class there is a Map classes property that contains objects of type org.hibernate.mapping.PersistentClass.

In a Grails application (which uses hibernate underneath the covers) I was extending a class that was extending the org.hibernate.cfg.Configuration. This allowed me to tap into the Map classes and do as follows:

for(PersistentClass pc : classes.values()) {
   for(Iterator iterator = pc.getTable().getForeignKeyIterator(); iterator.hasNext(); ) {
       ForeignKey fk = (ForeignKey) iterator.next();               
       iterator.remove();      
 }
}

Basically, I am removing the foreign keys to certain 3rd party tables.

I want to upgrade to Hibernate 5 but the new Configuration class does not have the Map classes that I depend on.

  1. How do I get the map of PersistentClass objects so that I can continue to remove their foreign key?

  2. If it is not supposed to be done through the Configuration anymore, how do I get access to the persistent objects? Through what class?

enter image description here

Please help. I am stuck.

Viriato
  • 2,981
  • 7
  • 40
  • 54
  • Possible duplicate of [Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cfg.Configuration.getClassMapping(className)](http://stackoverflow.com/questions/32780664/hibernate-migration-from-4-3-x-to-5-x-for-method-org-hibernate-cfg-configuration) – JimmyB Nov 29 '16 at 16:41
  • JimmyB it is not a duplicate. Please read both questions. Do you have any idea of how can I get a Map of all the PersistentClass objects in Hibernate 5? – Viriato Nov 29 '16 at 18:19
  • 1
    To me, it is a duplicate. Sort of. The answer to the other question shows how to get a hold of a `MetadataImplementor` from Hibernate. On that, you should be able to call, for instance, [`getEntityBindings()`](https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/boot/Metadata.html#getEntityBindings--) to get all `PersistentClass`es. What more do you need? – JimmyB Nov 30 '16 at 10:22

1 Answers1

1

i think first, we should get StandardServiceRegistry instance, like below:

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFileURL).build();

then we can get Metadata instance from this StandardServiceRegistry instance, like:

Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();

after that, we can get all PersistentClasses from that metaData instance, like below:

Collection<PersistentClass> entityBindings = metadata.getEntityBindings();

is this what you need?

yab
  • 267
  • 3
  • 13