序
本文主要研究下hibernate的hbm2ddl
SchemaManagementTool
hibernate-core-5.0.12.Final-sources.jar!/org/hibernate/tool/schema/spi/SchemaManagementTool.java
public interface SchemaManagementTool extends Service {
public SchemaCreator getSchemaCreator(Map options);
public SchemaDropper getSchemaDropper(Map options);
public SchemaMigrator getSchemaMigrator(Map options);
public SchemaValidator getSchemaValidator(Map options);
}
这个tool定义了create、drop、migrate、validate四个功能。
SchemaCreatorImpl
hibernate-core-5.0.12.Final-sources.jar!/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java
public class SchemaCreatorImpl implements SchemaCreator {
@Override
public void doCreation(Metadata metadata, boolean createNamespaces, List targets) throws SchemaManagementException {
doCreation( metadata, createNamespaces, targets.toArray( new Target[ targets.size() ] ) );
}
//......
}
主要逻辑在doCreation里头,里头按如下顺序创建:
创建catalog/schema
创建before table auxiliary objects
创建sequences
创建tables
创建indexes
创建uniques
创建foreign keys
创建after table auxiliary objects
它们又主要借助dialect的各种exporter来实现
Dialect
hibernate-core-5.0.12.Final-sources.jar!/org/hibernate/dialect/Dialect.java
public a