35

I want Hibernate make database file (SQLite), but only if not exists.

Now in hibernate.cfg.xml I have this line:

<property name="hibernate.hbm2ddl.auto">create</property>

The problem is that database file is been creating in all the time, even the file exists.

MAGx2
  • 3,149
  • 7
  • 33
  • 63
  • I tried this: create but it's always creating database. – MAGx2 May 28 '13 at 09:08
  • 1
    So apparently you haven't checked the hibernate manual, which explains the different variants of that option. Please remember that StackOverflow requires you to do a minimum of research yourself before posting a question. – creinig May 28 '13 at 09:17
  • I wrote a easy solution to follow link: http://stackoverflow.com/a/25549938/1736002 You can use 'import.sql' . So, if not exist the database, hibernate creates new db. – cguzel Aug 28 '14 at 13:32
  • 2
    It's pretty clear whats being asked here (unless you don't know Hibernate)! Don't know why this question was closed. The correct and accepted responses are further proof of that. – DavidR Jan 18 '18 at 06:40

2 Answers2

53

Try switching the value to update

<property name="hibernate.hbm2ddl.auto">update</property>
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 4
    I tried this on Hibernate 5 inside `persistence-unit::properties` and it didn't create the non-existing table `test` for my `@Entity` with `@Table(name = "test")`. – Sridhar Sarnobat Mar 13 '18 at 20:17
  • 1
    Same here. It does not create the database. Caused by: java.sql.SQLSyntaxErrorException: Unknown database – xdevx32 Jun 04 '20 at 06:08
37

Try This:

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

Difference between create and update is that, update will update your database if database tables are already created and will create if database tables are not created.

And create will create tables of database. And if tables are already created then it will drop all the tables and again create tables. In this case your data would be lost.

It is my personal advise to use update.

Abhendra Singh
  • 1,959
  • 4
  • 26
  • 46