I am new to quarkus and seem to be having a issue with the caching of hibernate in a unit test. The test has the 'UserTransaction' injected. The test should check a database cleanup task.
This is what to I do:
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);
Step 7 fails, because the 'find(id)' returns the entity, although it is not in the db anymore.
This does NOT happen if I skip step 5! So it seems to be caching issue to me.
I tried to Inject 'Session', 'SessionFactory' and 'EntitiyManager' to gain access to the current Hibernate Session, but none if this succeded.
Maybe the whole approach lacks something I didn`t get? How to make the world of entities match the database in a setup like mine?
Any hints and ideas are welcome.
TIA
The problem has a simple solution, add a transaction to the last find operation:
transaction.begin();
doc = Document.findById(uuid);
transaction.commit();
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