Spring – TDD with HSQLDB — removing foreign keys

foreign-keyshibernatehsqldbspringtdd

I'm using HSQLDB for data layer integration testing, which is great. However, I'm finding that my foreign key constraints are getting in the way of my tests. For example, to test a simple select on one table, I have to insert dummy data into five additional tables. This makes me want to throw things.

I have JPA annotations throughout our model code, and have configured Hibernate to recreate the schema (hbm2ddl.create-drop) in configuration. The joins are being interpreted correctly as foreign key constraints when the tables are generated.

What I'd like is to either:

  1. Not create the foreign keys initially (ideal, cleanest), or
  2. Find a way to programmatically drop all the foreign keys in the database (kinda hacky but will get the job done)

If it's helpful, I'm using Spring to autowire these tests. The tests in question inherit from AbstractTransactionalJUnit4SpringContextTests.

What do you think? Can this be done?

Best Answer

You can deactivate FK constraints with the following instruction:

SET REFERENTIAL_INTEGRITY FALSE;

You could execute it via a JDBC Statement before your test methods (and set it back to TRUE after).