简体   繁体   English

Hibernate 5.x迁移建议脚本

[英]Hibernate 5.x migration suggestions script

I'm trying to convert my working (with Hibernate 4.x) script that suggests migrations from the actual database. 我正在尝试转换我的工作脚本(使用Hibernate 4.x),该脚本建议从实际数据库进行迁移。 This is convenient expecially when I want track changes and store them in sql script used from FlyWay. 当我要跟踪更改并将其存储在FlyWay使用的sql脚本中时,这特别方便。

This is my class: 这是我的课:

public class SchemaGenerator {

private static Configuration cfg;
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/test?tinyInt1isBit=false";
private static String username = "root";
private static String password = "password";

public SchemaGenerator(String packageName) throws Exception {
    cfg = new Configuration();
    cfg.setProperty("hibernate.hbm2ddl.auto", "update");
    cfg.setProperty("hibernate.connection.driver_class", driver);
    cfg.setProperty("hibernate.connection.url", url);
    cfg.setProperty("hibernate.connection.username", username);
    cfg.setProperty("hibernate.connection.password", password);
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    Reflections reflections = new Reflections("com.test");
    Set<Class<? extends Object>> allClasses = reflections.getTypesAnnotatedWith(Entity.class);
    for (Class c : allClasses) {
        cfg.addAnnotatedClass(c);
    }

}

public static void validate() {
    SchemaValidator schemaValidator = new SchemaValidator(cfg);
    schemaValidator.validate();
}

public List<SchemaUpdateScript> createUpdateScript(DataSource dataSource, org.hibernate.dialect.Dialect dialect)
        throws SQLException {
    DatabaseMetadata meta = new DatabaseMetadata(dataSource.getConnection(), dialect, cfg);
    List<SchemaUpdateScript> createSQL = cfg.generateSchemaUpdateScriptList(dialect, meta);

    return createSQL;
}

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    SchemaGenerator gen = new SchemaGenerator("com.test");
    Driver driver = com.mysql.jdbc.Driver.class.newInstance();
    DataSource dataSource = new SimpleDriverDataSource(driver, url, username, password);

    List<SchemaUpdateScript> updateCommands = gen.createUpdateScript(dataSource,
            new org.hibernate.dialect.MySQL5Dialect());
    if (updateCommands.size() != 0)
        System.out.println("" + formatSqlSuggestionScript(updateCommands));
    else
        System.out.println("Already up to date");
}

private static String formatSqlSuggestionScript(List<SchemaUpdateScript> sqls) {
    StringBuilder builder = new StringBuilder("\n\n****update sql suggestion:***\n\n");
    for (SchemaUpdateScript str : sqls) {
        builder.append("\n" + str.getScript() + ";");
    }
    builder.append("\n\n");
    return builder.toString();
}

} }

I've some problems with schemaValidator.validate(); 我在schemaValidator.validate();遇到了一些问题schemaValidator.validate(); because now it expects Metadata and with cfg.generateSchemaUpdateScriptList(dialect, meta) that doesn't exist anymore. 因为现在它期望使用Metadata,并且具有cfg.generateSchemaUpdateScriptList(dialect, meta)不再存在。

Do you have some good hints to start with? 您有一些好的开始提示吗? Or maybe is there a better approach to do this now with Hibernate 5x? 也许现在有了Hibernate 5x更好的方法呢?

您可以实例化org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl并使用SchemaValidator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate从4.3.x迁移到5.x,用于方法org.hibernate.cfg.Configuration.getClassMapping(className) - Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cfg.Configuration.getClassMapping(className) 方法 org.hibernate.cache.spi.GeneralDataRegion.get(Object key) 从 4.3.x 到 5.x 的 Hibernate 迁移 - Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cache.spi.GeneralDataRegion.get(Object key) 集成器无法在Hibernate 5.x中正确加载 - Integrator not properly loading in Hibernate 5.x Hibernate 5.x 中的自定义标量值函数 - Custom Scalar Valued function in Hibernate 5.x 使用H2数据库的Junit 4.x + Hibernate 5.x. - Junit 4.x + Hibernate 5.x with H2 database Hibernate 5.x + Spring 5.x,无法自动装配DAOImpl类中的SessionFactory - Hibernate 5.x + Spring 5.x, can't autowire SessionFactory in DAOImpl class 在Hibernate 5.x中使用本机查询(更新查询)执行查询 - Execute query with native query (update query) in Hibernate 5.x 是否可以使用Hibernate Search 5.X对@Id字段使用数字编码 - Is it possible to use a numeric encoding for @Id fields with Hibernate Search 5.X Hibernate 5.x 中的 HQL 和 Criteria API 有什么区别? - What is difference between HQL and Criteria API in Hibernate 5.x? SessionImplementor是否可以访问Hibernate 5.x中的TransactionManager? - Does SessionImplementor have access to a TransactionManager in Hibernate 5.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM