title: SpringOrm-Hibernate 4 to 5 migration issue - need to mutate metadata before building sessionFactory
body: <p>I'm trying to upgrade from Hibernate 4.3.11 to 5.4.x with Spring-Orm 4.3.29. As part of the upgrade, I need to migrate existing custom logic that is dependent on NamedSQLQueries retrieved from Hibernate Configuration class. This custom logic mutates the NamedSQLQueries and updates back to the original Map before building the sessionFactory.
  In the new version (5.x) - I modified the bootstrapping as per the new documentation and able to build the metadata but not able to mutate the namedNativeQueries (per documentation metadata is immutable). Is there a way to mutate the metadata before building the sessionFactory.</p>
  <p>Current code -</p>
  <pre><code>public class CustomFactoryBean extends LocalSessionFactoryBean {
  ...
  
      @Override
      protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder localSessionFactoryBuilder) {
        localSessionFactoryBuilder.buildMappings(); //deprecated in 5.x
        mutateNamedSQLQueries(localSessionFactoryBuilder.getNamedSQLQueries()); //mutates NamedSQLQueries and updates back to the Map (Map&lt;String, NamedSQLQueryDefinition&gt;) AND localSessionFactoryBuilder.getNamedSQLQueries() returns nothing in 5.x (deprecated)
        localSessionFactoryBuilder.buildSessionFactory();
      }
  
  }
  </code></pre>
  <p>New Code -</p>
  <pre><code>    @Override
      protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder localSessionFactoryBuilder) {
  
              final BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder()
                      .enableAutoClose()
                      .applyIntegrator(MetadataExtractorIntegrator.INSTANCE) //MetadataExtractorIntegrator class is based on the 2nd link below
                      .build();
              
              final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bootstrapServiceRegistry)
                      .applySettings(localSessionFactoryBuilder.getProperties())
                      .build();
              
              MetadataSources metadataSources = new MetadataSources(serviceRegistry);
              metadataSources.addAnnotatedClass(TestEntity.class);
              
              Metadata metadata = metadataSources.buildMetadata();
              
              List&lt;NamedSQLQueryDefinition&gt; namedSQLQueryDefinitions = new ArrayList&lt;&gt;(metadata.getNamedNativeQueryDefinitions());
              mutateNamedNativeQueries(namedSQLQueryDefinitions); //not able to mutate namedSQLQueryDefinitions since metadata is immutable
  
              return metadata.buildSessionFactory();
  
      }
  
  </code></pre>
  <p>Per this link - <a href="https://hibernate.atlassian.net/browse/HHH-12089" rel="nofollow noreferrer">https://hibernate.atlassian.net/browse/HHH-12089</a>, looks like the right way to mutate metadata before building it is to use MetadataContributor but I don't see an example or any mention in the official documentation where and how to plugin the custom MetadataContributor.</p>
  <p>References -
  <em>Building the sessionFactory in Hibernate 5</em>
  <a href="https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#bootstrap-native-SessionFactory" rel="nofollow noreferrer">https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#bootstrap-native-SessionFactory</a></p>
  <p><em>To read the metadata once the sessionFactory is built</em>
  <a href="https://vladmihalcea.com/how-to-get-access-to-database-table-metadata-with-hibernate-5/" rel="nofollow noreferrer">https://vladmihalcea.com/how-to-get-access-to-database-table-metadata-with-hibernate-5/</a></p>
  <p>Any help on this would be much appreciated.</p>