How to install hibernate.hbm2ddl.auto in Spring using annotations and pure Java

As it would be to do the following setting in the Spring, using only Java and annotation.

<property name="hibernate.hbm2ddl.auto" value="update"/>

I should be possible, and I believe that it is much easier to do projects xml free.

PS: It should not be important, but I'm doing it on Heroku.

+4
source share
2 answers

I add this to the class, where the dataSource (), and he corrected my problem.

 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; } ; 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; } "); 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; } "); 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; } "); 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; } 

Full example here https://github.com/arose13/Heroku-Spring-Postgres-Example .

EDIT PS: for this line hibernateProperties.setProperty("hibernate.hbm2ddl.auto","update"); "); check out this question on steke_poverhnosti to find the best value if update isn 't for you.

+2
source

I do not think that there is a ready-to-use annotation for Java for hbm2ddl out of the box.

Hibernate uses standard annotations Java Persistance (JPA) plus some annotations expand Hibernate.

See: https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/

In general, I would rather recommend that you externalize option in the properties file, rather than hard-coding it into a Java class. Almost automatic generation circuit is usually something that you do only during a test / staging. Thus, this environment may be different with different settings.

0
source

All Articles