Hi guys,
I have a simple piece of code where I use and entity manager (2LC enabled with entities marked with @Cache(usage = Read_Write). I fetch 2 entities Media and Post, I detch them and then try to fetch them again. Against my expectations, the 2nd couple of fetches did go to the database, I was under the impression that the entities will be fetched from L2C. To acertain further that the first couple of fetches did result in the entites getting cached, I spawned another entity manager from the same factory and tried to retrieve the entity, my request went to the L2C and not the DB. Removing the detach call leads to to the 2nd couple of fetches going to the cache.
EntityManager entityManager = EMProvider.createCacheEnabledEntityManager();
entityManager.getTransaction().begin();
Media media1 = entityManager.find(Media.class, 1L);//load to 2nd LC
//EntityManager entityManager1 = EMProvider.createCacheEnabledEntityManager();
//entityManager.detach``(media1); Post post1 = entityManager.find(Post.class, 8L); System.out.println(post1); ``//entityManager.detach``(post1);
System.out.println(EMProvider.getCachedEMFactory().getCache().contains(Media.class, 1L));
System.out.println(EMProvider.getCachedEMFactory().getCache().contains(Post.class, 8L));
entityManager.find(Media.class, 1L);
entityManager.find(Post.class, 8L);
entityManager.getTransaction().commit();
entityManager.close();
entityManager = EMProvider.createCacheEnabledEntityManager();
System.out.println("************************************************************");
entityManager.getTransaction().begin();
entityManager.find(Media.class, 1L);
entityManager.find(Post.class, 8L);
entityManager.getTransaction().commit();
entityManager.close();