Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing Entire Database (for unit testing with Hibernate)

My unit tests use Hibernate to connect to an in-memory HSQLDB database. I was hoping there would be a way to clear and recreate the database (the entire database including the schema and all the data) in JUnit's TestCase.setUp() method.

like image 489
Jack Edmonds Avatar asked Sep 05 '10 15:09

Jack Edmonds


1 Answers

you can config your hibernate configuration file to force database to recreate your tables and schema every time.

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

hibernate.hbm2ddl.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.

e.g. validate | update | create | create-drop

if you don't like to have this config in your real hibernate config, you can create one hibernate config for unit testing purpose.

like image 185
mhshams Avatar answered Sep 19 '22 23:09

mhshams