Is the initialization/destruction of the schema necessarily tied to the session factory lifecycle? There might be cases which require to (re)-initialize the schema while an application is running; JPA 2.1 defines Persistence#generateSchema() which can be invoked in an ad-hoc fashion. Also I think whether to trigger schema initialization at SF start-up or not, should depend on the hibernate.hbm2ddl.auto setting.
So would an independent contract for schema initialization be more useful? It would provide hooks for creating/dropping all elements of the schema:
public interface SchemaInitializer {
void createIdentityGenerator(IdGeneratorKeyMetadata generatorKeyMetadata);
void dropIdentityGenerator(IdGeneratorKeyMetadata generatorKeyMetadata);
void createEntityTable(EntityDescriptor entityDescriptor); void dropEntityTable(EntityDescriptor entityDescriptor);
...
}
And be retrieved from the datastore provider and invoked at the right time:
public interface DatastoreProvider {
...
Class<? extends SchemaInitializer> getSchemaInitializerType();
}
This will move the decision when to invoke schema initialization/destruction and also the selection of the required contracts into core, leaving only the actual execution to the dialects.
|