Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: make database only if not exists [closed]

Tags:

java

hibernate

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.

like image 605
MAGx2 Avatar asked May 28 '13 09:05

MAGx2


People also ask

What is hbm2ddl Auto in Hibernate?

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.

Can Hibernate create the database automatically?

<property name="hibernate. hbm2ddl. auto">create</property> will create tables. But it will not create database.

Which property is used for Autocreation of tables in Hibernate?

A developer can have Hibernate create tables by adding the hbm2ddl. auto property and setting it to one of the following four values: validate. update.


2 Answers

Try switching the value to update

<property name="hibernate.hbm2ddl.auto">update</property> 
like image 181
Kevin Bowersox Avatar answered Sep 23 '22 17:09

Kevin Bowersox


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.

like image 30
Abhendra Singh Avatar answered Sep 20 '22 17:09

Abhendra Singh