在dropwizard-hibernate中生成模式

dgn*_*dgn 11 maven hibernate-4.x dropwizard

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

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

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

谢谢.


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

dgn*_*dgn 24

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

database:
    properties:
        hibernate.dialect: org.hibernate.dialect.MySQLDialect
        hibernate.hbm2ddl.auto: create
Run Code Online (Sandbox Code Playgroud)

(从这个答案)


老答案:

这就是我目前使用的:调用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);

    }

}
Run Code Online (Sandbox Code Playgroud)

我之前不知道hibernate工具.所以这个代码示例可以在服务初始化中使用hbm2ddl.auto = create.

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