1 回答
TA贡献2021条经验 获得超8个赞
由于一级缓存的概念,您创建并保存的初始对象与您加载的对象是同一个实例。由于您没有将创建的请求实体添加到内存模型中,而只是将其保存到数据库中,因此内存模型中不存在关系。
为了获得所需的结果,您需要清除保存和重新加载之间的持久性上下文。
@Test
public void createStreamWithSeveralStreamTargetsAndDeleteStream() {
List<StreamEntity> streamEntities = createStream(1);
List<RequestEntity> requestEntitySave = createRequest(1, streamEntityCheck);
assertEquals(1, requestEntitySave.size());
//you need to clear the persistence context here
//now a db query should execute.
//Inject the entitymanager to your test @PersistenceContext EntityManager em;
em.clear();
List<StreamEntity> streamEntities = streamDao.findAll();
assertEquals(1, streamEntities.size());
StreamEntity streamEntity2 = streamEntities.get(0);
streamEntity2.getRequestEntities(); - this collection shows 0
}
添加回答
举报