maven - 在 dropwizard-hibernate 中生成模式

标签 maven hibernate-4.x dropwizard

我按照 dropwizard 和 hibernate 的教程没有问题。现在我的实体中有重要的注释,我希望 hibernate 为我生成表,以及类似的东西。 那么,如何更改休眠的配置?我可以给它一个 hibernate.cfg.xml 吗?如果可以,是否需要重新设置连接?

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

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

谢谢你。

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

最佳答案

编辑:问题已解决!在 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。

关于maven - 在 dropwizard-hibernate 中生成模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17015143/

相关文章:

java - 如何在可执行 JAR 中加载 TestNG 配置?

ssl - 限制 Dropwizard 中的某些端点/路径

java - Freemarker:在模板中没有 getter 的情况下访问公共(public)字段

git - 如何依赖GitHub上Maven管理的项目?

maven - 我如何告诉 gradle 在 SNAPSHOT 版本上执行 uploadArchives?

maven - 在 Maven 中, `package:artifact:jar:version` 和包 :artifact:jar:tests:version`? 有什么区别

java - 在 Hibernate 中,StatelessSession 在有 EAGER JOIN 时防止过滤掉重复项

java - 根据 Criteria 设置懒惰的 child

java - Hibernate 从 3 个 SerializedBlob 类迁移到 4 个 SerializedBlob 类 已弃用

java - Dropwizard 健康检查超时?