Software Developer
Hibernate is the ORM tool given to transfer the data between a java (object) application and a database (Relational) in the form of the objects. Hibernate is a non-invasive framework, which means it won't force the programmers to extend/implement any class/interface, and in hibernate, we have all POJO classes so it's lightweight.
Metadata is "data that provides information about other data", but not the content of the data, such as the text of a message or the image itself. There are many distinct types of metadata, including:
Metadata is not strictly bound to one of these categories, as it can describe a piece of data in many other ways.
Hibernate could be very flexible, so it defines many psi (service issuer interfaces) that you could register to customize hibernate internals. The sort of interface is org.Hibernate.Integrator.Spi.Integrator is utilized by many technologies that combine with hibernate orm, like bean validation, Envers, or JACC Security Provider.
With the usage of the hibernate integrator API, we can write our issue that captures the session factory construct-time metadata which, in any other case, is most effective available all through bootstrap.
public class MetadataExtractorIntegrator
implements org.hibernate.integrator.spi.Integrator {
public static final MetadataExtractorIntegrator INSTANCE =
new MetadataExtractorIntegrator();
private Database database;
public Database getDatabase() {
return database;
}
@Override
public void integrate(
Metadata metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
database = metadata.getDatabase();
}
@Override
public void disintegrate(
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
}
}
The org.hibernate.boot.model.relational.The database is what we are interested in since it contains all the database-related metadata.
To register MetadataExtractorIntegrator with Hibernate we have two possibilities based on the bootstrap method.
If you’re using the Hibernate-native bootstrap, then you can register the Integrator with the BootstrapServiceRegistryBuilder as follows:
final BootstrapServiceRegistry bootstrapServiceRegistry =
new BootstrapServiceRegistryBuilder()
.enableAutoClose()
.applyIntegrator(MetadataExtractorIntegrator.INSTANCE)
.build();
final StandardServiceRegistry serviceRegistry =
new StandardServiceRegistryBuilder(bootstrapServiceRegistry)
.applySettings(properties())
.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 generates the following output:
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)
]
Hibernate is highly customizable, and the Integrator SPI allows you to get access to the Database metadata which you can later inspect from your enterprise application.