views:

168

answers:

2

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?

A: 

I would consider spending some time on creating a couple of fixtures, possibly with DBUnit, which you insert @Before.

BTW, AbstractTransactionalJUnit4Test is deprecated in Spring 3.0

Hans Westerbeek
re: deprecated... I meant "AbstractTransactionalJUnit4SpringContextTests" which I corrected above. FWIW we're using Spring 2.5.
roufamatic
+3  A: 

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).

Pascal Thivent
Thank you!!! You just single-handedly cut my test class in half.Interestingly this also makes it easier to root out where Hibernate is causing unnecessary inner joins.
roufamatic
For anyone using the AbstractTransactionalJUnit4SpringContextTests... the magic is this:`simpleJdbcTemplate.getJdbcOperations().execute("SET REFERENTIAL_INTEGRITY FALSE;");`
roufamatic
@roufamatic Glad you find it useful.
Pascal Thivent