@Before
  public void setUp() throws Exception {
    serviceRegistry =
        new StandardServiceRegistryBuilder()
            .enableAutoClose()
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();

    MetadataBuildingContext buildingContext =
        new MetadataBuildingContextTestingImpl(serviceRegistry);

    Properties properties = new Properties();
    properties.setProperty(SequenceGenerator.SEQUENCE, TEST_SEQUENCE);
    properties.setProperty(SequenceHiLoGenerator.MAX_LO, "3");
    properties.put(
        PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
        buildingContext.getObjectNameNormalizer());

    generator = new SequenceHiLoGenerator();
    generator.configure(
        StandardBasicTypes.LONG, properties, serviceRegistry.getService(JdbcEnvironment.class));

    Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    generator.registerExportables(metadata.getDatabase());

    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }
  @Override
  protected NameSpaceTablesInformation performTablesMigration(
      Metadata metadata,
      DatabaseInformation existingDatabase,
      ExecutionOptions options,
      Dialect dialect,
      Formatter formatter,
      Set<String> exportIdentifiers,
      boolean tryToCreateCatalogs,
      boolean tryToCreateSchemas,
      Set<Identifier> exportedCatalogs,
      Namespace namespace,
      GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation =
        new NameSpaceTablesInformation(
            metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());

    if (schemaFilter.includeNamespace(namespace)) {
      createSchemaAndCatalog(
          existingDatabase,
          options,
          dialect,
          formatter,
          tryToCreateCatalogs,
          tryToCreateSchemas,
          exportedCatalogs,
          namespace,
          targets);
      final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation(namespace);
      for (Table table : namespace.getTables()) {
        if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
          checkExportIdentifier(table, exportIdentifiers);
          final TableInformation tableInformation = tables.getTableInformation(table);
          if (tableInformation == null) {
            createTable(table, dialect, metadata, formatter, options, targets);
          } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
            tablesInformation.addTableInformation(tableInformation);
            migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
          }
        }
      }

      for (Table table : namespace.getTables()) {
        if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
          final TableInformation tableInformation = tablesInformation.getTableInformation(table);
          if (tableInformation == null
              || (tableInformation != null && tableInformation.isPhysicalTable())) {
            applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
            applyUniqueKeys(
                table, tableInformation, dialect, metadata, formatter, options, targets);
          }
        }
      }
    }
    return tablesInformation;
  }
  @Test
  public void testGeneratedUuidId() {
    StandardServiceRegistry ssr =
        new StandardServiceRegistryBuilder()
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();
    try {
      Metadata metadata =
          new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
      ((MetadataImpl) metadata).validate();

      PersistentClass entityBinding = metadata.getEntityBinding(TheEntity.class.getName());
      assertEquals(UUID.class, entityBinding.getIdentifier().getType().getReturnedClass());
      IdentifierGenerator generator =
          entityBinding
              .getIdentifier()
              .createIdentifierGenerator(
                  metadata.getIdentifierGeneratorFactory(),
                  metadata.getDatabase().getDialect(),
                  null,
                  null,
                  (RootClass) entityBinding);
      assertTyping(UUIDGenerator.class, generator);

      // now a functional test
      SessionFactory sf = metadata.buildSessionFactory();
      try {
        TheEntity theEntity = new TheEntity();

        Session s = sf.openSession();
        s.beginTransaction();
        s.save(theEntity);
        s.getTransaction().commit();
        s.close();

        assertNotNull(theEntity.id);

        s = sf.openSession();
        s.beginTransaction();
        s.delete(theEntity);
        s.getTransaction().commit();
        s.close();
      } finally {
        try {
          sf.close();
        } catch (Exception ignore) {
        }
      }
    } finally {
      StandardServiceRegistryBuilder.destroy(ssr);
    }
  }
  @Before
  public void setUp() throws Exception {
    BasicTestingJdbcServiceImpl jdbcServices = new BasicTestingJdbcServiceImpl();
    jdbcServices.prepare(false);

    serviceRegistry =
        new StandardServiceRegistryBuilder()
            .enableAutoClose()
            .addService(JdbcEnvironment.class, jdbcServices.getJdbcEnvironment())
            .addService(JdbcServices.class, jdbcServices)
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();

    generator = new SequenceHiLoGenerator();

    // Build the properties used to configure the id generator
    Properties properties = new Properties();
    properties.setProperty(SequenceGenerator.SEQUENCE, TEST_SEQUENCE);
    properties.setProperty(SequenceHiLoGenerator.MAX_LO, "0"); // JPA allocationSize of 1
    properties.put(
        PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
        new ObjectNameNormalizer() {
          @Override
          protected MetadataBuildingContext getBuildingContext() {
            return new MetadataBuildingContextTestingImpl(serviceRegistry);
          }
        });
    generator.configure(StandardBasicTypes.LONG, properties, serviceRegistry);

    final Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    metadata
        .getDatabase()
        .addAuxiliaryDatabaseObject(
            new SimpleAuxiliaryDatabaseObject(
                Collections.<String>emptySet(),
                null,
                null,
                generator.sqlCreateStrings(TestingDatabaseInfo.DIALECT),
                generator.sqlDropStrings(TestingDatabaseInfo.DIALECT)));

    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }