首页 > 解决方案 > 无法使用 SchemaExport 生成脚本文件

问题描述

我有一个非常简单的脚本来为我的数据库生成我的 DLL:

public static void main(String[] args) {
    
   Map<String, String> settings = new HashMap<>();
   settings.put("connection.driver_class", "com.mysql.jdbc.Driver");
   settings.put("dialect", "org.hibernate.dialect.MySQLDialect");
   settings.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydatabase");
   settings.put("hibernate.connection.username", "root");
   settings.put("hibernate.connection.password", "root");
   settings.put("hibernate.show_sql", "true");
   settings.put("hibernate.format_sql", "true");
    
   ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
       .applySettings(settings)
       .build();
   MetadataSources metadata = new MetadataSources(serviceRegistry);
   metadata.addAnnotatedClass(AddressEntity.class);
   metadata.addAnnotatedClass(CompanyEntity.class);
    
   EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);
   SchemaExport schemaExport = new SchemaExport();
   schemaExport.setOutputFile("myScript.sql");
   schemaExport.execute(enumSet, SchemaExport.Action.BOTH, metadata.buildMetadata());
}

如果我查看 DEBUG 和 INFO 信息,它正在工作。我可以看到 DLL 语句被打印出来。

我唯一的问题是将它导出到文件中。我已经尝试了一切,但setOutputFile似乎对我不起作用。我无法生成脚本。

标签: hibernate

解决方案


这一行:

EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);

应更正为:

EnumSet<TargetType> enumSet = EnumSet.of(TargetType.SCRIPT);

推荐阅读