Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hibernate.hbm2ddl.auto in Spring with Annotations and pure Java

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.

like image 647
SARose Avatar asked Oct 01 '15 22:10

SARose


People also ask

What is hbm2ddl Auto in hibernate?

hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly.


1 Answers

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.

like image 139
SARose Avatar answered Oct 19 '22 11:10

SARose