在dropwizard-hibernate中生成模式

时间:2013-06-09 23:21:37

标签: maven hibernate-4.x dropwizard

我按照dropwizard和hibernate的教程没有遇到任何问题。现在我在我的实体中有非平凡的注释,我希望hibernate为我生成表格,以及类似的东西。那么,我怎样才能改变hibernate的配置呢?我可以给它一个hibernate.cfg.xml吗?如果可以的话,我是否必须再次建立连接?

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

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

谢谢。


编辑:我从另一个角度处理问题,显式创建架构而不是使用hbm2ddl.auto。见建议的答案。

1 个答案:

答案 0 :(得分:24)

编辑:问题解决了!在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);

    }

}

之前我不知道hibernate工具。因此,此代码示例可用于服务初始化,以像hbm2ddl.auto = create一样运行。

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