How to discover fully qualified table column from Hibernate MetadataSources

前端 未结 1 543
日久生厌
日久生厌 2021-02-05 23:34

I have an entity, for which I have a Class reference:

@Entity
class MyEntity {
    @Id int id;
    @Column String col1;
    @Column(         


        
相关标签:
1条回答
  • 2021-02-06 00:16

    The org.hibernate.boot.Metadata is what we are interested in since it contains the PersistentClass entity bindings.

    First, you need to create an Integrator which will give you access to Metadata:

    public class MetadataExtractorIntegrator 
        implements org.hibernate.integrator.spi.Integrator {
     
        public static final MetadataExtractorIntegrator INSTANCE = 
            new MetadataExtractorIntegrator();
     
        private Database database;
     
        private Metadata metadata;
     
        public Database getDatabase() {
            return database;
        }
     
        public Metadata getMetadata() {
            return metadata;
        }
     
        @Override
        public void integrate(
                Metadata metadata,
                SessionFactoryImplementor sessionFactory,
                SessionFactoryServiceRegistry serviceRegistry) {
     
            this.database = metadata.getDatabase();
            this.metadata = metadata;
     
        }
     
        @Override
        public void disintegrate(
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {
     
        }
    }
    

    If you use JPA, you can register it as follows:

    Map<String, Object> configuration = new HashMap<>();
     
    Integrator integrator = integrator();
    if (integrator != null) {
        configuration.put("hibernate.integrator_provider", 
            (IntegratorProvider) () -> Collections.singletonList(
                MetadataExtractorIntegrator.INSTANCE
            )
        );
    }
     
    EntityManagerFactory entityManagerFactory = new EntityManagerFactoryBuilderImpl(
        new PersistenceUnitInfoDescriptor(persistenceUnitInfo), 
        configuration
    )
    .build();
    

    Now, when running the following test case:

    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    
    for ( PersistentClass persistentClass : metadata.getEntityBindings()) {
     
        Table table = persistentClass.getTable();
         
        LOGGER.info( "Entity: {} is mapped to table: {}",
                     persistentClass.getClassName(),
                     table.getName()
        );
     
        for(Iterator propertyIterator = persistentClass.getPropertyIterator(); 
                propertyIterator.hasNext(); ) {
            Property property = (Property) propertyIterator.next();
             
            for(Iterator columnIterator = property.getColumnIterator(); 
                    columnIterator.hasNext(); ) {
                Column column = (Column) columnIterator.next();
                 
                LOGGER.info( "Property: {} is mapped on table column: {} of type: {}",
                             property.getName(),
                             column.getName(),
                             column.getSqlType()
                );
            }
        }
    }
    

    Against on the following entities:

    We get the following output:

    Entity: com.vladmihalcea.book.hpjp.util.providers.entity.BlogEntityProvider$Tag is mapped to table: tag
    Property: name is mapped on table column: name of type: varchar(255)
    Property: version is mapped on table column: version of type: integer
     
    Entity: com.vladmihalcea.book.hpjp.util.providers.entity.BlogEntityProvider$PostComment is mapped to table: post_comment
    Property: post is mapped on table column: post_id of type: bigint
    Property: review is mapped on table column: review of type: varchar(255)
    Property: version is mapped on table column: version of type: integer
     
    Entity: com.vladmihalcea.book.hpjp.util.providers.entity.BlogEntityProvider$Post is mapped to table: post
    Property: title is mapped on table column: title of type: varchar(255)
    Property: version is mapped on table column: version of type: integer
     
    Entity: com.vladmihalcea.book.hpjp.util.providers.entity.BlogEntityProvider$PostDetails is mapped to table: post_details
    Property: createdBy is mapped on table column: created_by of type: varchar(255)
    Property: createdOn is mapped on table column: created_on of type: datetime(6)
    Property: version is mapped on table column: version of type: integer
    

    Cool, right?

    You can check out this example on GitHub as well.

    0 讨论(0)
提交回复
热议问题