Skip to content

setCacheRetrieveMode(BYPASS) not honored on EntityManager and Query #33189

@njr-11

Description

@njr-11

Jakarta Persistence 3.2 added a setCacheRetrieveMode to EntityManager and Query, but EclipseLink is not honoring it.

Note that EclipseLink does honor setting the value as a Hint,

            query.setHint("jakarta.persistence.cache.retrieveMode",
                          CacheRetrieveMode.BYPASS);

but does not honor setting the same value instead with the newer methods.

Here is an example where it fails (retrieving the original value of 222 instead of the updated value of 444),

        String id = "testPersistenceUnitEMUpdateAndRetrieveInTran";
        tx.begin();
        try (EntityManager em = persistenceUnitRepo.entityManager()) {
            PersistenceUnitEntity e = PersistenceUnitEntity.of(id, 222);
            e = em.merge(e);
            em.flush();
        } finally {
            tx.commit();
        }

        tx.begin();
        try (EntityManager em = persistenceUnitRepo.entityManager()) {
            // TODO the following is not honored:
            em.setCacheRetrieveMode(CacheRetrieveMode.BYPASS);

            Query update = em.createQuery("""
                            UPDATE PersistenceUnitEntity
                               SET value = value * 2
                             WHERE id = ?1
                            """);
            update.setParameter(1, id);
            assertEquals(1L, update.executeUpdate());

            Query query = em.createQuery("""
                              FROM PersistenceUnitEntity
                             WHERE id = ?1
                            """);
            query.setParameter(1, id);

            // TODO the following is not honored:
            query.setCacheRetrieveMode(CacheRetrieveMode.BYPASS);
            // TODO This currently only works if the following is done instead:
            //query.setHint("jakarta.persistence.cache.retrieveMode",
            //              CacheRetrieveMode.BYPASS);

            List<?> results = query.getResultList();
            assertEquals(1,
                         results.size());
            PersistenceUnitEntity e = (PersistenceUnitEntity) results.get(0);
            assertEquals(Integer.valueOf(444),
                         e.value);
        } finally {
            if (tx.getStatus() == Status.STATUS_ACTIVE)
                tx.commit();
            else
                tx.rollback();
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions