如何在Spring中仅使用Java和注释设置以下内容。如何在Spring中使用注释和纯Java设置hibernate.hbm2ddl.auto
<property name="hibernate.hbm2ddl.auto" value="update"/>
我阿米特应该是可能的,我相信这是一个更加简洁,使项目的XML免费。 PS:这应该不重要,但我在Heroku上运行它。
如何在Spring中仅使用Java和注释设置以下内容。如何在Spring中使用注释和纯Java设置hibernate.hbm2ddl.auto
<property name="hibernate.hbm2ddl.auto" value="update"/>
我阿米特应该是可能的,我相信这是一个更加简洁,使项目的XML免费。 PS:这应该不重要,但我在Heroku上运行它。
将此添加到dataSource()所在的类中,它解决了我的问题。
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "true");
return hibernateProperties;
}
完整的例子是https://github.com/arose13/Heroku-Spring-Postgres-Example。
编辑PS:对于此行hibernateProperties.setProperty("hibernate.hbm2ddl.auto","update");
看看这个stackoverflow question找出最佳值,如果update
是不适合你。
我不认为有一个开箱即用的用于hbm2ddl的Java注释。
Hibernate使用标准的Java Persistence Annotations(JPA)加上一些Hibernate扩展注释。
参见:https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/
一般来说,我宁愿建议你外在化在属性文件中的设置而不是Java类中的硬编码。实际上,自动生成图表通常是您在测试/分级过程中只会执行的操作。所以你可能有不同的环境和不同的设置。