Search in sources :

Example 6 with IndirectCollection

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) DatabaseValueHolder(org.eclipse.persistence.internal.indirection.DatabaseValueHolder) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery) TypedQuery(jakarta.persistence.TypedQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) IndirectCollection(org.eclipse.persistence.indirection.IndirectCollection) Dealer(org.eclipse.persistence.testing.models.jpa.advanced.Dealer) Session(org.eclipse.persistence.sessions.Session) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

IndirectCollection (org.eclipse.persistence.indirection.IndirectCollection)6 EntityManager (jakarta.persistence.EntityManager)4 HashMap (java.util.HashMap)3 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)3 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)3 Query (jakarta.persistence.Query)2 ValueHolder (org.eclipse.persistence.indirection.ValueHolder)2 ValueHolderInterface (org.eclipse.persistence.indirection.ValueHolderInterface)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 LoadGroup (org.eclipse.persistence.queries.LoadGroup)2 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)2 TypedQuery (jakarta.persistence.TypedQuery)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 IndirectList (org.eclipse.persistence.indirection.IndirectList)1 DescriptorIterator (org.eclipse.persistence.internal.descriptors.DescriptorIterator)1 DatabaseValueHolder (org.eclipse.persistence.internal.indirection.DatabaseValueHolder)1