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.
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.
<property name="hibernate. hbm2ddl. auto">create</property> will create tables. But it will not create database.
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.
Try switching the value to update
<property name="hibernate.hbm2ddl.auto">update</property>
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With