Java – a simple way to truncate all tables and clear the first and second level hibernate caches?
•
Java
I'm writing some integration tests for the spring / Hibernate application I'm studying. I want to test as close to the real conditions as possible, including using hibernate's L2 cache and submitting transactions
I wonder if there is an effective way for hibernate to delete everything from the database and cache The best thing I can think of is to use HQL "delete from ximpl" line for each type of object, but I have dozens of domain objects, so I think there should be a better method
Solution
For databases, recreate the schema using the schemaexport tool:
Configuration cfg = ....; new SchemaExport(cfg).create(false,true);
For L2 cache, you can access the underlying cache area from sessionfactory and evict all contents:
SessionFactory sf = ...; Cache cache = sf.getCache(); cache.evictEntityRegions(); cache.evictCollectionRegions(); cache.evictQueryRegions();
For the first level cache, just get a new session or call session clear().
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码