I’m writing some integration tests for a Spring/Hibernate app I’m working on, and I would like to to test things with as close to real conditions as possible, which includes using Hibernate’s second level cache and committing transactions.
I was wondering if there was an efficient way to ask Hibernate to delete everything from the database and the caches. The best I could come up with was using an HQL “delete from XImpl” line for every type of object, but I have a couple dozen domain objects, and it feels like there should be a better way.
For the database, use the
SchemaExporttool to recreate the schema:For the 2nd level cache, get access to the underlying cache regions from the
SessionFactoryand evict everything:For the 1st level cache, well, simply get a new
Sessionor callsession.clear().