2008年7月6日 星期日

根據hibernate configuration file建立table schema

程式範例如下:

package mytest;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class SchemaExportTester {

public static void main(String[] args) {
new SchemaExportTester().createSchema();
}

private void createSchema() {
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setOutputFile("schema.sql");
schemaExport.create(true, true);
System.out.print("ok");
}
}


其中
schemaExport.create(true, true);
第一個參數是表示是否要產生schema.sql,第二個參數是表示是否要在database立建立table。