How to get access to database table metadata with Hibernate 5?

How to get access to database table metadata with Hibernate 5?
Techiio-author
Written by Sagar RabidasFebruary 22, 2022
10 min read
Hibernate
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

How to get access to database table metadata with Hibernate 5 - Today start the discussion with this topic

Introduction of Hibernate & Metadata:-

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:

  • Descriptive metadata- the descriptive information about a resource. It is used for discovery and identification. It includes elements such as title, abstract, author, and keywords.
  • Structural metadata- metadata about containers of data and indicates how compound objects are put together, for example, how pages are ordered to form chapters. It describes the types, versions, relationships, and other characteristics of digital materials.
  • Administrative metadata- the information to help manage a resource, like a resource type, permissions, and when and how it was created.
  • Reference metadata- the information about the contents and quality of statistical data.
  • Statistical metadata- also called process data, may describe processes that collect, process, or produce statistical data.
  • Legal metadata- provides information about the creator, copyright holder, and public licensing, if provided.

Metadata is not strictly bound to one of these categories, as it can describe a piece of data in many other ways.

Integrator:-

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.

Hibernate-native bootstrap:-

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();

Domain Model:-

When running the following test case:

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)
]

Conclusion:-

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.

Hibernate
metadata
database
java
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance