title: How should I register custom Hibernate 5 data type (BasicType) when Spring Data is used?
body: <p>I use Spring Data and decided that I want to create new custom data type that can be used in Hibernate entities. I checked the documentation and choose <code>BasicType</code> and implemented it according to this <a href="http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#basic-custom-type" rel="noreferrer">official user guide</a>. </p>
  
  <p>I wanted to be able to register the type under its class name and be able to use the new type in entities without need for <code>@Type</code> annotation. Unfortunately, I’m unable to get reference to the <code>MetadataBuilder</code> or Hibernate configuration to register the new type. Is there a way how to get it in Spring Data? It seems that initialization of the Hibernate is hidden from the user and cannot be easily accessed. We use following class to initialize the JPA:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(
          entityManagerFactoryRef = "entityManagerFactory",
          transactionManagerRef = "transactionManager",
          basePackages = {
                  "..." // omitted
          }
  )
  public class JpaConfiguration implements TransactionManagementConfigurer {
  
      @Primary
      @Bean(name = "entityManagerFactory")
      public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory(
          DataSource dataSource,
          SchemaPerTenantConnectionProviderImpl provider) {
  
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactoryBean.setPersistenceUnitName("defaultPersistenceUnit");
          entityManagerFactoryBean.setDataSource(dataSource);
          entityManagerFactoryBean.setPackagesToScan(
                  "..." // omitted
          );
          entityManagerFactoryBean.setJpaProperties(properties(provider));
          entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
          return entityManagerFactoryBean;
      }
  
      @Primary
      @Bean(name = "transactionManager")
      public PlatformTransactionManager annotationDrivenTransactionManager() {
          return new JpaTransactionManager();
      }
  
      private Properties properties(SchemaPerTenantConnectionProviderImpl provider) {
          Properties properties = new Properties();
          // omitted
          return properties;
      }
  }
  </code></pre>
  
  <p>I found lots of articles about way how to do it with Hibernate’s Configuration object but this one refers to Hibernate 3 and 4. I also found way how to do it via Hibernate <code>org.hibernate.integrator.spi.Integrator</code> but when I use it according to the articles I found I will get exception with the message <em>“org.hibernate.HibernateException: Can not alter TypeRegistry at this time”</em>
  What is the correct way to register custom types in Spring Data?</p>