persistence
persistence copied to clipboard
Allow passing in an entity graph on EntityManager.refresh
trafficstars
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.
This would be useful, and will be easier to in light of the direction we've decided to take #383.