use of org.eclipse.persistence.indirection.IndirectCollection in project eclipselink by eclipse-ee4j.
the class AdvancedJPAJunitTest method testTransparentIndirectionValueHolderSessionReset.
/**
* Bug 470161 - ServerSession links RepeatableWriteUnitOfWork via entity / IndirectList / QueryBasedValueHolder\
*
* Through the UoW: Persist a new object, read an existing object in (building from rows, original not put in the
* shared cache), associate existing object with new object, commit. Previously, after the changes are merged
* into the shared cache, the transparent collection on the existing object has a wrapped VH from the initial uow
* query, which internally references the uow session, not the shared session.
*/
public void testTransparentIndirectionValueHolderSessionReset() {
Employee emp = null;
Dealer dealer = null;
// setup
EntityManager em = createEntityManager();
try {
beginTransaction(em);
dealer = new Dealer();
dealer.setFirstName("Angle");
dealer.setLastName("Bracket");
em.persist(dealer);
commitTransaction(em);
} finally {
closeEntityManager(em);
// start test with an empty cache
clearCache();
}
// test
em = createEntityManager();
try {
beginTransaction(em);
emp = new Employee();
emp.setFemale();
emp.setFirstName("Case");
emp.setLastName("Statement");
em.persist(emp);
Query query = em.createQuery("select d from Dealer d where d.firstName = :firstName and d.lastName = :lastName");
query.setParameter("firstName", dealer.getFirstName());
query.setParameter("lastName", dealer.getLastName());
List<Dealer> resultsList = query.getResultList();
assertTrue("List returned should be non-empty", resultsList.size() > 0);
Dealer dealerFound = resultsList.get(0);
emp.addDealer(dealerFound);
commitTransaction(em);
// verify valueholder configuration in shared cache
Session parentSession = JpaHelper.getServerSession(em.getEntityManagerFactory());
Dealer cachedDealer = (Dealer) parentSession.getIdentityMapAccessor().getFromIdentityMap(dealer);
assertNotNull("Dealer with id should be in the cache: " + dealer.getId(), cachedDealer);
ClassDescriptor descriptor = parentSession.getDescriptor(Dealer.class);
DatabaseMapping mapping = descriptor.getMappingForAttributeName("customers");
IndirectCollection indirectCollection = (IndirectCollection) mapping.getAttributeValueFromObject(cachedDealer);
assertFalse("Collection VH should be uninstantiated", indirectCollection.isInstantiated());
DatabaseValueHolder dbValueHolder = (DatabaseValueHolder) indirectCollection.getValueHolder();
assertFalse("Referenced VH should be uninstantiated", dbValueHolder.isInstantiated());
Session vhSession = dbValueHolder.getSession();
assertSame("Dealer.customers VH session should reference the shared session", parentSession, vhSession);
} finally {
closeEntityManager(em);
}
// reset
em = createEntityManager();
try {
beginTransaction(em);
emp = em.find(Employee.class, emp.getId());
if (emp != null) {
em.remove(emp);
}
dealer = em.find(Dealer.class, dealer.getId());
if (dealer != null) {
em.remove(dealer);
}
commitTransaction(em);
} finally {
closeEntityManager(em);
}
}
Aggregations