【发布时间】:2020-07-05 19:47:37
【问题描述】:
我是 quarkus 的新手,似乎在单元测试中遇到了休眠缓存的问题。 测试注入了“UserTransaction”。 该测试应检查数据库清理任务。
这就是我要做的:
- 创建实体
- 开始交易
- 使用“persistAndFlush”保存遵循“Active Record”模式的实体
- 提交事务
- 尝试通过“find(id)”从数据库中获取实体,以确保它已被保存
- 运行清理任务(实体从数据库中删除)
- 再次尝试通过“find(id)”从数据库中获取实体,以确保它已被删除
Document doc;
UUID uuid;
doc = new Document();
uuid = UUID.randomUUID();
doc.uuid = uuid;
doc.doc = new byte[0];
doc.createdAt = Instant.now();
transaction.begin();
doc.persistAndFlush();
transaction.commit();
doc = Document.findById(uuid);
Assertions.assertNotNull(doc);
TimeUnit.SECONDS.sleep(Long.parseLong(maxAge)+1);
scheduler.cleanUp();
doc = Document.findById(uuid);
Assertions.assertNull(doc);
第 7 步失败,因为“find(id)”返回实体,尽管它不再在数据库中。
如果我跳过第 5 步,这不会发生!所以这对我来说似乎是缓存问题。
我尝试注入 'Session'、'SessionFactory' 和 'EntitiyManager' 以获取对当前 Hibernate Session 的访问权限,但如果成功则没有。
也许整个方法缺少我没有得到的东西? 如何在像我这样的设置中使实体世界与数据库匹配?
欢迎任何提示和想法。
TIA
【问题讨论】:
-
你能告诉我们你的代码吗?
-
问题已更新。
标签: java hibernate caching quarkus