【问题标题】:How do I export schema after upgrading Hibernate to 5.4.1?将 Hibernate 升级到 5.4.1 后如何导出架构?
【发布时间】:2019-02-21 14:57:24
【问题描述】:

我最近将 Hibernate 从 4.3.7 更新到 5.4.1,SchemaExport API 从 5.1 开始发生了变化。此代码现在显示编译问题(在SchemaExport 构造函数和execute 方法上)。

/**
 * Method to generate a SQL script which aim is to create SQL tables for the
 * entities indicated as parameters.
 */
private void generateScript(Class<?>... classes) {
    Configuration configuration = new Configuration();
    configuration.setProperty(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString());
    for (Class<?> entityClass : classes) {
        configuration.addAnnotatedClass(entityClass);
    }
    SchemaExport schemaExport = new SchemaExport(configuration);
    schemaExport.setDelimiter(SCRIPT_DELIMITER);
    schemaExport.setOutputFile(getScriptPath());
    schemaExport.setFormat(true);
    boolean consolePrint = false;
    boolean exportInDatabase = false;
    schemaExport.execute(consolePrint, exportInDatabase, false, true);
}

我已经看到与此问题相关的其他问题,但没有足够具体的内容来帮助我重写此函数。

【问题讨论】:

    标签: java hibernate schemaexport


    【解决方案1】:

    这是我所做的并且它有效:

    private void genererScript(Class<?>... classes) {
    
        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                .applySetting(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString())
                .build();
    
        MetadataSources sources = new MetadataSources( standardRegistry );
        for (Class<?> entityClass : classes) {
            sources.addAnnotatedClass(entityClass);
        }
    
        MetadataImplementor metadata = (MetadataImplementor) sources
                .getMetadataBuilder()
                .build();
    
        EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
    
        try {
            Files.delete(Paths.get(getScriptPath()));
        } catch (IOException e) {
            /*
             * The file did not exist...
             * we do nothing.
             */
        }
    
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.setDelimiter(SCRIPT_DELIMITER);
        schemaExport.setOutputFile(getScriptPath());
        schemaExport.setFormat(true);
        schemaExport.createOnly(targetTypes, metadata);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-22
      • 2011-01-31
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      相关资源
      最近更新 更多