Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all tables at hibernate startup

Tags:

java

hibernate

How can I configure hibernate in a way that it DROPS all tables before it runs, for developement purposes? I don't want to constantly DROP all tables by hand before running my code.

I'm already using

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

But this does not seem to work, since I ran into several errors that where caused by wrong schemas from a previous run.

Edit Maybe my questions was a bit missleading. I want to have a clean database. Its not only because of actual runtime errors but also about having a clean database for every run.

<property name="hbm2ddl.auto">create-drop</property>

Ignores tables that are unknown to hibernate (which can happen quite often if you are playing around with tablenames).

like image 646
Chris Avatar asked Aug 20 '13 18:08

Chris


Video Answer


2 Answers

Use the value create-drop instead of create

like image 120
Antoniossss Avatar answered Oct 02 '22 13:10

Antoniossss


Another possibility to get an always clean database at startup in a development environment is an in memory db like H2 or similar. See http://www.h2database.com/html/features.html#in_memory_databases for details.

like image 31
Salevajo Avatar answered Oct 02 '22 13:10

Salevajo