简单的方法来截断所有表,清除第一级和第二级休眠缓存? [英] Easy way to truncate all tables, clear first and second level hibernate cache?
问题描述
我正在为我正在编写的Spring / Hibernate应用程序编写一些集成测试,并且我想尽可能接近真实条件来测试,包括使用Hibernate的二级缓存和提交事务。
我想知道是否有一种有效的方法可以让Hibernate从数据库和缓存中删除所有内容。我能想到的最好的办法是使用HQL从XImpl中删除行来查看每种类型的对象,但我有几十个域对象,感觉应该有更好的方法。
$ b $对于数据库,使用 SchemaExport
工具重新创建模式: $ b $ b
配置cfg = ....;
新的SchemaExport(cfg).create(false,true);
对于二级缓存,可以从访问底层缓存区域, SessionFactory
并逐出所有:
SessionFactory sf = ...;
高速缓存= sf.getCache();
cache.evictEntityRegions();
cache.evictCollectionRegions();
cache.evictQueryRegions();
对于一级缓存,只需获得一个新的 Session
或者调用
session.clear()
。
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 SchemaExport
tool to recreate the schema:
Configuration cfg = ....;
new SchemaExport(cfg).create(false, true);
For the 2nd level cache, get access to the underlying cache regions from the SessionFactory
and evict everything:
SessionFactory sf = ...;
Cache cache = sf.getCache();
cache.evictEntityRegions();
cache.evictCollectionRegions();
cache.evictQueryRegions();
For the 1st level cache, well, simply get a new Session
or call session.clear()
.
这篇关于简单的方法来截断所有表,清除第一级和第二级休眠缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!