How to avoid caching of entites in Spring Repository during a Transaction for tests

前端 未结 1 1194
时光取名叫无心
时光取名叫无心 2021-02-05 19:29

I have a Test like this:

@Transactional
@Test
public void addRemoveTest() {
    MyEntitiy entity = new MyEntitiy ();

    entity = textureRepository.saveAndFlush         


        
1条回答
  •  粉色の甜心
    2021-02-05 20:02

    The only way I know is to call entityManager.clear() before calling the finder. I think you can autowire the EntityManager into your test:

    @Autowired
    private EntityManager entityManager;
    

    Your test would then look like this:

    @Transactional
    @Test
    public void addRemoveTest() {
        MyEntitiy entity = new MyEntitiy ();
    
        entity = textureRepository.saveAndFlush(entity );
        entityManager.clear();
        final MyEntitiy loadedTexture = myEntityRepository.findOne(entity .getId());
    }
    

    0 讨论(0)
提交回复
热议问题