6

How would one go about setting up the following in Spring using only Java and annotations.

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

I amIt should be possible and I do believe it is a lot cleaner to make projects xml free.

PS: This shouldn't be important but I'm running this on Heroku.

SARose
  • 3,558
  • 5
  • 39
  • 49

2 Answers2

5

Added this to the class where the dataSource() is and it fixed my issue.

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 is here https://github.com/arose13/Heroku-Spring-Postgres-Example.

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

Community
  • 1
  • 1
SARose
  • 3,558
  • 5
  • 39
  • 49
0

I don't think there is a ready to use annotation for Java for hbm2ddl out of the box.

Hibernate uses the standard Java Persistance Annotations (JPA) plus some Hibernate extension annotations.

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

Generally I would rather recommend you externalise the setting in a property file rather then hardcoding it in the Java class. Practically auto schema generation is usually something you would only do during test/staging. So you might have different environments with different settings for this.

seba.wagner
  • 3,800
  • 4
  • 28
  • 52