问题
I have over 200 unit tests for my entire JPA backend and they all work great on hsql db 2.2. I have the tests running in a continuous build environment to verify the stability of the system. Except that when I ran them on Oracle 11, some tests failed with referential integrity constraints. It was due to a bug in my code but the fact that I couldn't find them in hsqldb concerns me and violates the whole point of having a lightweight-in-memory-jpa-compliant db when it's not really compliant!
Is there a setting that I can trigger for hsqldb to consider referntial integrity while running tests. I suspect they are ignored by default. These are my jdbc settings:
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:SampleProject
jdbc.username=sa
jdbc.password=
回答1:
You can use: "SET DATABASE REFERENTIAL INTEGRITY FALSE" and "SET DATABASE REFERENTIAL INTEGRITY TRUE" sql statements
回答2:
HSQLDB is very strict on referential integrity, unless it is turned off. JPA implementations actually use different strategies in this area for different databases. For example, with one database engine the implementation may define foreign keys with cascading deletes and delete the parent object, but for another engine it may delete the children of an object separately, before deleting the parent.
Therefore, HSQLDB as the development database helps you find the errors to a large extent, but you still need to validate your software by running against the target database.
Going back to the specific question, if you run one of your tests with a file database like this:
jdbc.url=jdbc:hsqldb:file:/home/db/SampleProject;hsqldb.write_delay=false;shutdown=true
You can then open the database with the DatabaseManager after the test and check the table and constraint definitions. If the JPA does create foreign keys, you can check for yourself that the contraints are enforced by HSQLDB if you try some inserts and deletes.
来源:https://stackoverflow.com/questions/6525713/how-to-enable-referential-integrity-in-hsql-db-in-memory-unit-tests