hibernate 的 SchemaExport 实现

来源:互联网 发布:mac 终端退出 bash 编辑:程序博客网 时间:2024/05/17 07:56

在 控制台 输出sql 语句:


new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);  


该类 SchemaExport 的方法的主要作用是  将表drop之后,再重新 create表 。


首先 SchemaExport 的构造函数 用到了 AnnotationConfiguration的 configure() 方法,


configure() 方法返回的两个参数   stream, 和 resource 都和 xml配置文件有关:

return doConfigure( stream, resource );

public SchemaExport(Configuration cfg) throws HibernateException {
this( cfg, cfg.getProperties() );
}


在  SchemaExport 构造的时候,存在 根据sql的方言来决定用哪种 sql 的 语法。


connectionHelper = new ManagedProviderConnectionHelper( props );

来获取到数据库的连接; 

dropSQL = cfg.generateDropSchemaScript( dialect );

得到drop 表的sql 语句


createSQL = cfg.generateSchemaCreationScript( dialect );

得到 generate 表的sql 语句


public SchemaExport(Configuration cfg, Properties properties)
throws HibernateException {
dialect = Dialect.getDialect( properties );


Properties props = new Properties();
props.putAll( dialect.getDefaultProperties() );
props.putAll( properties );


connectionHelper = new ManagedProviderConnectionHelper( props );
dropSQL = cfg.generateDropSchemaScript( dialect );
createSQL = cfg.generateSchemaCreationScript( dialect );
format = PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props );
}


另外注意一个 final class Environment ,其中的一些配置 如 

public static final String USER ="hibernate.connection.username";

public static final String DIALECT ="hibernate.dialect"; 都是 从hibernate.cfg.xml获得的 。


还有注意一下:类Configuration 的 protected Map tables; 参数怎么初始化的:

这些参数都是 通过 DOM4j的方法 从 hibernate.cfg.xml获得的,具体是在 new AnnotationConfiguration().configure() 的 configure()方法中 。








1 0
原创粉丝点击