* as it currently lacks the ability to distinguish fetch plan configuration in its key value.
* The PCtx's default fetch plan is modified by the "openjpa.FetchGroups" property.
*
*/
public void testFinderCache004() {
OpenJPAEntityManagerFactory emf2 = createNamedEMF(getPersistenceUnitName(),
FGManager.class, FGDepartment.class, FGEmployee.class, FGAddress.class,
"openjpa.FetchGroups", "default,DescFetchGroup");
OpenJPAEntityManager em = emf2.createEntityManager();
FetchPlan fp = em.getFetchPlan();
assertNotNull(fp);
assertNotNull(fp.getFetchGroups());
assertEquals(2, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertTrue(fp.getFetchGroups().contains("DescFetchGroup"));
FetchConfiguration fetchCfg = ((org.apache.openjpa.persistence.EntityManagerImpl) em)
.getBroker()
.getFetchConfiguration();
assertTrue(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
FGManager mgr = managerSet.iterator().next();
assertNotNull(mgr);
{
// First find, to prime the Finder Cache
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertEquals(mgr.getDescription(), findMgr.getDescription()); // Should not be lazy-loaded
}
{
// Second find, should rely on the finder cache to reuse generated SQL.
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertEquals(mgr.getDescription(), findMgr.getDescription()); // Should not be lazy-loaded
}
// Remove a fetch group to the fetch plan and verify expected behavior
fp.removeFetchGroup("DescFetchGroup");
assertNotNull(fp.getFetchGroups());
assertEquals(1, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertFalse(fp.getFetchGroups().contains("DescFetchGroup"));
assertFalse(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
{
// Second find, should rely on the finder cache to reuse generated SQL.
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertNull(findMgr.getDescription()); // Should be lazy-loaded
}
// Restore the fetch group previously removed, and verify expected behavior
fp.addFetchGroup("DescFetchGroup");
assertNotNull(fp.getFetchGroups());
assertEquals(2, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertTrue(fp.getFetchGroups().contains("DescFetchGroup"));
assertTrue(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
{
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertEquals(mgr.getDescription(), findMgr.getDescription()); // Should not be lazy-loaded
}
// Remove a fetch group to the fetch plan and verify expected behavior
fp.removeFetchGroup("DescFetchGroup");
assertNotNull(fp.getFetchGroups());
assertEquals(1, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertFalse(fp.getFetchGroups().contains("DescFetchGroup"));
assertFalse(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
{
// Second find, should rely on the finder cache to reuse generated SQL.
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertNull(findMgr.getDescription()); // Should be lazy-loaded
}
// Reset the fetch plan, and verify expected behavior
fp.resetFetchGroups();
assertNotNull(fp.getFetchGroups());
assertEquals(2, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertTrue(fp.getFetchGroups().contains("DescFetchGroup"));
assertTrue(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
{
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertEquals(mgr.getDescription(), findMgr.getDescription()); // Should not be lazy-loaded
}
// Clear all fetch groups, and verify expected behavior
// OPENJPA-2413: now places "default" in the list of active fetch groups.
fp.clearFetchGroups();
assertNotNull(fp.getFetchGroups());
assertEquals(1, fp.getFetchGroups().size());
assertTrue(fp.getFetchGroups().contains("default"));
assertFalse(fp.getFetchGroups().contains("DescFetchGroup"));
assertFalse(fetchCfg.isDefaultPUFetchGroupConfigurationOnly());
{
FGManager findMgr = em.find(FGManager.class, mgr.getId());
em.clear();
assertEquals(mgr.getId(), findMgr.getId());
assertEquals(mgr.getFirstName(), findMgr.getFirstName());
assertEquals(mgr.getLastName(), findMgr.getLastName());
assertNull(findMgr.getDescription()); // Should be lazy-loaded
}
em.close();
emf2.close();
}