I have a Test like this:
@Transactional
@Test
public void addRemoveTest() {
MyEntitiy entity = new MyEntitiy ();
entity = textureRepository.saveAndFlush(entity );
TestTransaction.flagForCommit();
TestTransaction.end();
TestTransaction.start();
final MyEntitiy loadedTexture = myEntityRepository.findOne(entity .getId());
}
This works perfectly fine. But when I remove the Committing code for the transaction, the repository will not call the database when calling findOne()
. Can I somehow force it to make the database call for my tests?
I prefer my test to not commit any transaction.
I'm not able to get the Session
to clear the cache. And I'm not even sure if clearing the session would help.
The usage of the @Repository annotation or @Transactional . @Repository is not needed at all as the interface you declare will be backed by a proxy the Spring Data infrastructure creates and activates exception translation for anyway.
To disable second-level caching (say for debugging purposes), we just set the hibernate. cache. use_second_level_cache property to false.
And since caching is not part of Spring Data JDBC and Spring Data JDBC repositories are just Spring Beans, you can combine it with any caching solution you like. The obvious choice is of course, Springs Caching abstraction behind which you can put any caching solution.
Spring provides two ways to evict a cache, either by using the @CacheEvict annotation on a method, or by auto-wiring the CacheManger and clearing it by calling the clear() method.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With