persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Allow passing in an entity graph on EntityManager.refresh

Open trajano opened this issue 10 months ago • 1 comments

At present I do the following to reload an entity such that all the fields in the entity graph provided are loaded

    entityManager.detach(entity);
    final var entityType = entityManager.getMetamodel().entity(entity.getClass());
    final var entityGraph = entityManager.getEntityGraph(entityType.getName());
    return (T)
        entityManager.find(
            entity.getClass(),
            entity.getId(),
            Map.of(
                "jakarta.persistence.loadgraph", // or "jakarta.persistence.fetchgraph"
                entityGraph,
                "jakarta.persistence.cache_retrieve_mode",
                CacheRetrieveMode.BYPASS));

It would be cleaner I think if I can simply do

    final var entityType = entityManager.getMetamodel().entity(entity.getClass());
    final var entityGraph = entityManager.getEntityGraph(entityType.getName());
    entityManager.refresh(entity, 
            Map.of(
                "jakarta.persistence.loadgraph", // or "jakarta.persistence.fetchgraph"
                entityGraph));
   // and no cache retrieve mode

To reload the entity with the graph that was provided.

trajano avatar Aug 09 '23 11:08 trajano