Delete all tables when hibernation starts

How to configure hibernate so that it deletes all tables before starting, for development purposes? I do not want to constantly DROP all tables manually before running my code.

I already use

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

But this does not work, as I encountered several errors that caused the wrong circuits from the previous run.

Edit Perhaps my questions were a bit skipped. I want to have a clean database. This is not only due to real runtime errors, but also because of the availability of a clean database for each run.

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

Ignores tables that are unknown for hibernation (which can happen quite often if you play with tabs).

+4
source share
4 answers

Use create-drop value instead of create

+2
source

Another option to always get a clean database when starting up in a development environment is db memory such as H2 or similar. See http://www.h2database.com/html/features.html#in_memory_databases for more details.

+1
source
 <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create-drop</property> 
0
source

You, the Java configuration file, reference the database here:

enter image description here

So, the part "postgres" in "jdbc: postgresql: // localhost: 5432 / postgres" of your configuration file refers to the red box that I painted here in PostgreSQL / pgAdmin:

enter image description here

I suggest you create a new database and move the schemas and tables that you do not want Hibernate to touch this database.

Since every time hibernate connects to pgAdmin, it will access the database that you set in your configuration file, and the settings that you have in the configuration file will be applied to this database.

To create a new database in pgAdmin, right-click on Databases and go to "New Database". Here is the image:

enter image description here

0
source

Source: https://habr.com/ru/post/1498063/


All Articles