【问题标题】:Generate schema in dropwizard-hibernate在 dropwizard-hibernate 中生成模式
【发布时间】:2013-06-05 14:13:57
【问题描述】:

我按照 dropwizard 和 hibernate 的教程进行操作,没有问题。现在我的实体中有重要的注释,我希望 hibernate 为我生成表,以及类似的东西。那么,我怎样才能更改 hibernate 的配置?我可以给它一个hibernate.cfg.xml吗?如果可以,是否必须重新建立连接?

我找到了这个PR, 但它似乎还没有公开发布(我的 jar 中没有 hibernateBundle.configure)

但也许我正在寻找错误的东西。到目前为止,我只是尝试设置 hibernate.hbm2dll.auto。毕竟,可能还有其他方法可以在 Dropwizard 中启用休眠表生成...那么,有什么帮助吗?

谢谢。


编辑:我从另一个角度解决了这个问题,明确地创建架构而不是使用 hbm2ddl.auto。请参阅建议的答案。

【问题讨论】:

  • 我也有同样的问题。我认为你可以在类路径中提供persistence.xml,它应该可以工作

标签: maven hibernate-4.x dropwizard


【解决方案1】:

编辑:问题解决了!在 YAML 配置中执行此操作目前有效:(Dropwizard 0.7.1)

database:
    properties:
        hibernate.dialect: org.hibernate.dialect.MySQLDialect
        hibernate.hbm2ddl.auto: create

(来自this answer


旧答案:

这是我目前正在使用的:一个调用hibernate的SchemaExport将架构导出到SQL文件或修改数据库的类。我只是在更改实体后运行应用程序之前运行它。

public class HibernateSchemaGenerator {

    public static void main(String[] args) {
        Configuration config = new Configuration();

        Properties properties = new Properties();

        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/db"); 
        properties.put("hibernate.connection.username", "user");
        properties.put("hibernate.connection.password", "password");
        properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        properties.put("hibernate.show_sql", "true");
        config.setProperties(properties);

        config.addAnnotatedClass(MyClass.class);

        SchemaExport schemaExport = new SchemaExport(config);

        schemaExport.setOutputFile("schema.sql");
        schemaExport.create(true, true);

    }

}

我以前不知道休眠工具。所以这个代码示例可以在服务初始化中使用,就像hbm2ddl.auto = create一样。

我目前只是通过运行类(来自 eclipse 或 maven)来生成和查看输出 SQL 来使用它。

【讨论】:

  • 在文档中找不到。生成模式的命令是什么?
猜你喜欢
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 2019-09-22
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多