. , .
Integrator
:
public class MetadataExtractorIntegrator
implements org.hibernate.integrator.spi.Integrator {
public static final MetadataExtractorIntegrator INSTANCE =
new MetadataExtractorIntegrator();
private Database database;
@Override
public void integrate(
Metadata metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
database = metadata.getDatabase();
}
@Override
public void disintegrate(
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
}
public Database getDatabase() {
return database;
}
}
Hibernate .
Hibernate, :
final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder();
bsrb.enableAutoClose();
Integrator integrator = integrator();
if (integrator != null) {
bsrb.applyIntegrator( integrator );
}
final BootstrapServiceRegistry bsr = bsrb.build();
final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr);
JPA, :
protected EntityManagerFactory newEntityManagerFactory() {
PersistenceUnitInfo persistenceUnitInfo = persistenceUnitInfo(
getClass().getSimpleName()
);
Map<String, Object> configuration = new HashMap<>();
configuration.put("hibernate.integrator_provider",
(IntegratorProvider) () -> Collections.singletonList( MetadataExtractorIntegrator.INSTANCE )
);
EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = new EntityManagerFactoryBuilderImpl(
new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration
);
return entityManagerFactoryBuilder.build();
}
:
for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE
.getDatabase()
.getNamespaces()) {
for( Table table : namespace.getTables()) {
LOGGER.info( "Table {} has the following columns: {}",
table,
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(
table.getColumnIterator(),
Spliterator.ORDERED
),
false
)
.collect( Collectors.toList())
);
}
}
Hibernate :
Table org.hibernate.mapping.Table(post) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(title),
org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_comment) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(review),
org.hibernate.mapping.Column(version),
org.hibernate.mapping.Column(post_id)
]
Table org.hibernate.mapping.Table(post_details) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(created_by),
org.hibernate.mapping.Column(created_on),
org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_tag) has the following columns: [
org.hibernate.mapping.Column(post_id),
org.hibernate.mapping.Column(tag_id)
]
Table org.hibernate.mapping.Table(tag) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(name),
org.hibernate.mapping.Column(version)
]
!