创建数据库之后,可以为此使用SchemaExport导出要在新创建的数据库中创建的实体。基本步骤如下。如何获取配置的属性并不重要。
Configuration config = new Configuration(); config.addAnnotatedClass(Class1.class); config.addAnnotatedClass(Class2.class); config.addAnnotatedClass(Class3.class); <set all hibernate properties/datasource here> SchemaExport schema = new SchemaExport(config); schema.create(true, true);
Javadocs在这里:http
:
//docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html
有关设置配置的选项,请参见此处。http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/cfg/Configuration.html
编辑: 我想有一点要补充的一点是,让hibernate模式在生产环境中处理DB / SCHEMA /
TABLE创建被认为是不好的做法。根据需要和可行性,最好为此保存准备好的SQL语句,甚至由数据库管理员手动进行。但是,由于我们都很懒惰,我想那通常不会发生。;
D



