Load entity graph is ignored when entity to load is aready in session cache.
This code apply entity graph corectly to loaded entity correctly : {code} entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); //Relaod same product but now providing an EntityGraph with contents EntityGraph<ProductDL> eg=entityManager.createEntityGraph(ProductDL.class); eg.addSubgraph("contents"); Map<String,Object> props=new HashMap<String,Object>(); props.put( "javax.persistence.loadgraph", eg); productDL=entityManager.find(ProductDL.class,productDL.getId(),props); entityManager.getTransaction().commit(); entityManager.close(); productDL.getContents(); {code}
This code doesnt works. Before read entity with Entity Graph, we load entity so its stored in session cache. When entity is readed with entity graph it is returned from session cache wihouth applying Entity Graph
{code} entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); //Load product and store it in session cache //Removing this line code works well due to "Product 1" dont be cached in session cache! entityManager.find(ProductDL.class,productDL.getId()); //Relaod same product but now providing an EntityGraph with contents EntityGraph<ProductDL> eg=entityManager.createEntityGraph(ProductDL.class); eg.addSubgraph("contents"); Map<String,Object> props=new HashMap<String,Object>(); props.put( "javax.persistence.loadgraph", eg); productDL=entityManager.find(ProductDL.class,productDL.getId(),props); entityManager.getTransaction().commit(); entityManager.close(); //Product without resolve its contents productDL.getContents(); {code} |
|