Access entity version before commit transaction



I have a usecase where I need to write the updated entity version to the database before commiting the transaction:

1. Change entity field e.g. from a to b
2. Create a change entry in database containing the new version of the entity

So I need to access the version of an updated entity inside the transaction before commiting it. How can I achieve this? If I add something to a collection field of the entity ObjectDB seems to update the version field automatically. But if I just set a String field it doesn´t. I tried transaction.flush() in combination with transaction.refresh(entity) which works but I wonder if there is an easier approach.

My fake code is:

        Transactional transactional = context.getInstance(Transactional.class);
        String id = transactional.execute(transaction -> {
            Flight flight = transaction.createEntity(Flight.class);
            // flight.version is 1
            return flight.getId();

        transactional.execute(transaction -> {
            Flight flight = transaction.getEntity(id, false);
            // here I need to access the new version, but I get still 1
            // now I get the right version 2

        transactional.execute(transaction -> {
            Flight flight = transaction.getEntity(id, false);
            // in this case ObjectDB seems to automatically update the version since it returns 3 here

Hint: transactional.execute() just creates a transaction (entityManager.getTransaction().begin();) and commits it automatically at the end (entityManager.getTransaction().commit();).

Thanks for your help!

Best wishes,


Use enhanced classes to get instant change tracking of all fields. They are also faster.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login