A version 5 Hibernate SchemaExport example with the ServiceRegistry and Metadata

The manner in which you create a version 5 Hibernate SchemaExport object is markedly different than earlier versions of the framework. But it’s not overwhelmingly difficult. Here’s how you do it.

Hibernate and JPA database settings

First, you shove all your database properties into a HashMap or Hashtable:

Map<String, String> settings = new HashMap<>();
settings.put("connection.driver_class", "com.mysql.jdbc.Driver");
settings.put("dialect", "org.hibernate.dialect.MySQLDialect");
settings.put("hibernate.connection.url", "jdbc:mysql://localhost/hibernate_examples");
settings.put("hibernate.connection.username", "root");
settings.put("hibernate.connection.password", "password");

Hibernate SchemaExport ServiceRegistry creation

Then you create a ServiceRegistry object using these database settings:

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                                  .applySettings(settings).build();

Configure Hibernate metadata

You need to tell the Hiberante SchemaExport class about all of your entity beans, which is done through the MetadataSources object:

MetadataSources metadata = new MetadataSources(serviceRegistry);
//metadata.addAnnotatedClass(Player.class);

Set TargetType and Action enums

A couple of annotations then come into play. The TargetType allows you to instruct the SchemaExport class to target a database as opposed to just writing database creation SQL to a script. And the Action enumeration allows you to specify whether to create the database, drop the database or do both. Feed these two enums to the execute method of the Hibernate SchemaExport class and the JBoss framework will create the various database tables and colums that your JPA entities need in order to persist their state beyond the scope of the Java application.

EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);
SchemaExport schemaExport = new SchemaExport();
schemaExport.execute(enumSet, Action.BOTH, metadata.buildMetadata());

One thing to note is that the process for Hibernate 5 SchemaExport creation is remarkably similar to the creation of the Hibernate SessionFactory from which the EntityManager can be obtained:

Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionFactory = 
    configuration.buildSessionFactory(serviceRegistry);
sessionFactory.getCurrentSession();

The version 5 Hibernate SchemaExport example

Here’s how a complete class with a main method that makes a Hibernate 5 SchemaExport execute call to create database tables looks in its entirety:

package com.mcnz.jpa.examples;

import java.util.*;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaExport.Action;
import org.hibernate.tool.schema.TargetType;

public class MyDatabaseWizard {

  public static void main(String[] args) {
    Map<String, String> settings = new HashMap<>();
    settings.put("connection.driver_class", 
                 "com.mysql.jdbc.Driver");
        
    settings.put("dialect", 
                 "org.hibernate.dialect.MySQLDialect");
    settings.put("hibernate.connection.url", 
                 "jdbc:mysql://localhost/hibernate_examples");
        
    settings.put("hibernate.connection.username", "root");
    settings.put("hibernate.connection.password", "password");
    settings.put("hibernate.show_sql", "true");
    settings.put("hibernate.format_sql", "true");

    ServiceRegistry serviceRegistry = 
      new StandardServiceRegistryBuilder().applySettings(settings).build();

    MetadataSources metadata = 
      new MetadataSources(serviceRegistry);
    metadata.addAnnotatedClass(Player.class);

    EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);
    SchemaExport schemaExport = new SchemaExport();
    schemaExport.execute(enumSet, Action.BOTH, metadata.buildMetadata());
        
    //Configuration configuration = new Configuration();
    //configuration.configure();
    //SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    //sessionFactory.getCurrentSession();
        
  }
}

You can find the source code for this example on GitHub.