Maven 在dropwizard hibernate中生成架构

Maven 在dropwizard hibernate中生成架构,maven,hibernate-4.x,dropwizard,Maven,Hibernate 4.x,Dropwizard,我遵循dropwizard和hibernate的教程,没有遇到任何问题。现在我的实体中有了非平凡的注释,我想让hibernate为我生成表之类的东西。那么,如何更改hibernate的配置呢?我可以给它一个hibernate.cfg.xml吗?如果可以,是否需要重新设置连接? 我发现了这个, 但它似乎还没有公开发行(在我的jars中没有hibernateBundle.configure) 但也许我找错东西了。到目前为止,我只是想设置hibernate.hbm2dll.auto。毕竟,在Dropw

我遵循dropwizard和hibernate的教程,没有遇到任何问题。现在我的实体中有了非平凡的注释,我想让hibernate为我生成表之类的东西。那么,如何更改hibernate的配置呢?我可以给它一个hibernate.cfg.xml吗?如果可以,是否需要重新设置连接?

我发现了这个, 但它似乎还没有公开发行(在我的jars中没有hibernateBundle.configure)

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

多谢各位



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

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

(来自)


旧答案:

这就是我目前使用的:一个调用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。

我也有同样的问题。我认为您可以只在类路径中提供persistence.xml,它应该在文档中找不到。生成模式的命令是什么?
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);

    }

}