While migrating from Hibernate 4 to 5 I came across the deprecation and eventual removal of the SchemaExport(Configuration)
constructor. What is a good alternative in Hibernate 5?
Use case
During testing we create a SchemaExport
instance with a configuration that had some properties set and defines mapping resources:
// somewhere else `Properties` are filled and passed as a parameter
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
// more properties ...
Configuration configuration = new Configuration();
configuration.setProperties(testProperties);
// parameter `String... mappingResources`
for (final String mapping : mappingResources) {
configuration.addResource(mapping);
}
// this doesn't compile
SchemaExport schemaExport = new SchemaExport(configuration);
The last line does not compile on Hibernate 5 because the constructor was removed.
Options
The deprecation recommends to use the SchemaExport(MetadataImplementor)
constructor, but I struggle to find a good way to create a MetadataImplementor
instance. I found a few options, but they all look fishy to me.
The only concrete implementations in Hibernate that I could find are MetadataImpl
and InFlightMetadataCollectorImpl
, but both are in org.hibernate.boot.internal
, so I assume I'm not supposed to use them. Also, MetadataImpl
has a massive constructor in which I need to provide every little detail and InFlightMetadataCollectorImpl
needs a MetadataBuildingOptions
, which has the same problems as MetadataImplementor
(implementation is internal and hard to construct).
Alternatively, it looks like MetadataBuilderImpl
might be a handy way to construct a MetadataImplementor
, but it's also internal.
Either way, I couldn't find out how to set Properties
(or their entries) on a MetadataImplementor
(or MetadataBuilderImpl
for that matter).
Question
Is a MetadataImplementor
really required to create a SchemaExport
? If so, how do I get one from a supported API and how do I set Properties
?
Eventually we want to execute a script with execute
, but the signature changed here as well. I see it now takes a ServiceRegistry
, so maybe that would be a way out?
EDIT
Argh, I just saw that in 5.2 (which I want to use) SchemaExport
does not even take a MetadataImplementor
any longer - only the parameterless constructor remains. What now?