6 thoughts on “How to get access to database table metadata with Hibernate 5

  1. Really good hint.

    But doesn’t the MetadataExtractorIntegrator implementation needs to assign the Metadata/Database instance to the static INSTANCE rather than to this? Otherwise it’s impossible to pick it later from there…

    And, for picking it up in the BootstrapServiceRegistry, I personally prefer using the ServiceLoader API, i.e. having a META-INF/services/org.hibernate.integrator.spi.Integrator file in the classpath containing the FQN of the implementing class. Then you don’t need any custom bootstrapping code at all.

  2. Thanks for creating a blog post for my question. I have one more requirement. If I want to get the entity class name to which the table is mapped, then using the same logic that you mentioned, is it correct if I do it like this as I want to return the Class object also.
    Eg: Class cls = Class.forName(table.getQuotedName());
    And if i want to get the entity variable names to which the columns are mapped, can I get it column.getQuotedName()?
    Thanks!

      1. I see that getEntityBinding takes the entity name as string, I have the DB table name with me. Considering your example, where post_details is the table name that I have, I want to get the entity class (Eg: PostDetails.java) object.

        public Class getEntityClassByTableName(String tableName) {

        Class cls = null;
        for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE.getDatabase().getNamespaces()) {

        for( Table table : namespace.getTables()) {
           if(table.getName().equals(tableName)) {
               //how can i get the entity class name as the Table class does not yield that to use the 
               //metadata.getEntityBinding() which you suggested.
             }
        }
        

        }
        return cls;
        }

        Thanks!

      2. Take your time to study the API and you’ll find that you can get all EntityBindings from the Metadata. You can filter them by their associated getTable property. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *