Search in sources :

Example 6 with IndirectList

use of org.eclipse.persistence.indirection.IndirectList in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testForUOWInSharedCacheWithBatchQueryHint.

/**
 * This test ensures that the eclipselink.batch query hint works. It tests
 * two things.
 *
 * 1. That the batch read attribute is properly added to the queyr 2. That
 * the query will execute
 *
 * It does not do any verification that the batch reading feature actually
 * works. That is left for the batch reading testing to do.
 */
public void testForUOWInSharedCacheWithBatchQueryHint() {
    if (isOnServer()) {
        // Can not unwrap on WLS.
        return;
    }
    int id1 = 0;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee manager = new Employee();
    manager.setFirstName("Marvin");
    manager.setLastName("Malone");
    PhoneNumber number = new PhoneNumber("cell", "613", "888-8888");
    manager.addPhoneNumber(number);
    number = new PhoneNumber("home", "613", "888-8880");
    manager.addPhoneNumber(number);
    em.persist(manager);
    id1 = manager.getId();
    Employee emp = new Employee();
    emp.setFirstName("Melvin");
    emp.setLastName("Malone");
    emp.setManager(manager);
    manager.addManagedEmployee(emp);
    number = new PhoneNumber("cell", "613", "888-9888");
    emp.addPhoneNumber(number);
    number = new PhoneNumber("home", "613", "888-0880");
    emp.addPhoneNumber(number);
    em.persist(emp);
    emp = new Employee();
    emp.setFirstName("David");
    emp.setLastName("Malone");
    emp.setManager(manager);
    manager.addManagedEmployee(emp);
    number = new PhoneNumber("cell", "613", "888-9988");
    emp.addPhoneNumber(number);
    number = new PhoneNumber("home", "613", "888-0980");
    emp.addPhoneNumber(number);
    em.persist(emp);
    commitTransaction(em);
    closeEntityManager(em);
    clearCache();
    // org.eclipse.persistence.jpa.JpaQuery query =
    // (org.eclipse.persistence.jpa.JpaQuery)em.createQuery("SELECT e FROM Employee e WHERE e.lastName = 'Malone' order by e.firstName");
    em = createEntityManager();
    beginTransaction(em);
    try {
        org.eclipse.persistence.jpa.JpaQuery query = (org.eclipse.persistence.jpa.JpaQuery) em.createQuery("SELECT e FROM Employee e WHERE e.lastName = 'Malone' order by e.firstName");
        query.setHint(QueryHints.BATCH, "e.phoneNumbers");
        List resultList = query.getResultList();
        emp = (Employee) resultList.get(0);
        emp.setFirstName("somethingelse" + System.currentTimeMillis());
        // execute random other query
        em.createQuery("SELECT e FROM Employee e WHERE e.lastName = 'Malone'").getResultList();
        ((Employee) resultList.get(1)).getPhoneNumbers().hashCode();
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
    try {
        emp = (Employee) JpaHelper.getEntityManager(em).getDatabaseSession().getIdentityMapAccessor().getFromIdentityMap(emp);
        assertNotNull("Error, phone numbers is empty.  Not Batch Read", emp.getPhoneNumbers());
        assertFalse("PhoneNumbers was empty.  This should not be the case as the test created phone numbers", emp.getPhoneNumbers().isEmpty());
        assertTrue("Phonee numbers was not an indirectList", emp.getPhoneNumbers() instanceof IndirectList);
        assertNotNull("valueholder was null in triggered batch attribute", ((IndirectList) emp.getPhoneNumbers()).getValueHolder());
        BatchValueHolder bvh = (BatchValueHolder) ((IndirectList) emp.getPhoneNumbers()).getValueHolder();
        if (bvh.getQuery() != null && bvh.getQuery().getSession() != null && bvh.getQuery().getSession().isUnitOfWork()) {
            fail("In Shared Cache a UOW was set within a BatchValueHolder's query object");
        }
        closeEntityManager(em);
    } finally {
        em = createEntityManager();
        beginTransaction(em);
        try {
            emp = em.find(Employee.class, id1);
            Iterator<Employee> it = emp.getManagedEmployees().iterator();
            while (it.hasNext()) {
                Employee managedEmp = it.next();
                it.remove();
                managedEmp.setManager(null);
                em.remove(managedEmp);
            }
            em.remove(emp);
            commitTransaction(em);
        } finally {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
        }
    }
}
Also used : BatchValueHolder(org.eclipse.persistence.internal.indirection.BatchValueHolder) IndirectList(org.eclipse.persistence.indirection.IndirectList) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) IndirectList(org.eclipse.persistence.indirection.IndirectList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with IndirectList

use of org.eclipse.persistence.indirection.IndirectList in project eclipselink by eclipse-ee4j.

the class SimpleSerializeFetchGroupTests method verifyUnfetchedAttributes.

@Test
public void verifyUnfetchedAttributes() throws Exception {
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        TypedQuery<Employee> q = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(p.id) FROM PhoneNumber p)", Employee.class);
        FetchGroup fg = new FetchGroup("Employee.empty");
        q.setHint(QueryHints.FETCH_GROUP, fg);
        Employee emp = q.getSingleResult();
        assertNotNull(emp);
        assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        // This check using the mapping returns a default (empty) IndirectList
        /*OneToManyMapping phoneMapping = (OneToManyMapping) employeeDescriptor.getMappingForAttributeName("phoneNumbers");
            IndirectList phones = (IndirectList) phoneMapping.getAttributeValueFromObject(emp);
            assertNotNull(phones);
            assertTrue(phones.isInstantiated());
            assertEquals(0, phones.size());
            assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());*/
        IndirectList phonesIL = (IndirectList) emp.getPhoneNumbers();
        assertFalse(phonesIL.isInstantiated());
        assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        assertTrue(emp.getPhoneNumbers().size() > 0);
        assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) IndirectList(org.eclipse.persistence.indirection.IndirectList) Test(org.junit.Test)

Example 8 with IndirectList

use of org.eclipse.persistence.indirection.IndirectList in project eclipselink by eclipse-ee4j.

the class SimpleSerializeFetchGroupTests method verifyUnfetchedAttributes.

@Test
public void verifyUnfetchedAttributes() throws Exception {
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        TypedQuery<Employee> q = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(p.owner.id) FROM PhoneNumber p)", Employee.class);
        FetchGroup fg = new FetchGroup("Employee.empty");
        q.setHint(QueryHints.FETCH_GROUP, fg);
        Employee emp = q.getSingleResult();
        assertNotNull(emp);
        assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        // This check using the mapping returns a default (empty) IndirectList
        /*OneToManyMapping phoneMapping = (OneToManyMapping) employeeDescriptor.getMappingForAttributeName("phoneNumbers");
            IndirectList phones = (IndirectList) phoneMapping.getAttributeValueFromObject(emp);
            assertNotNull(phones);
            assertTrue(phones.isInstantiated());
            assertEquals(0, phones.size());
            assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());*/
        IndirectList phonesIL = (IndirectList) emp.getPhoneNumbers();
        assertFalse(phonesIL.isInstantiated());
        assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        assertTrue(emp.getPhoneNumbers().size() > 0);
        assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) IndirectList(org.eclipse.persistence.indirection.IndirectList) Test(org.junit.Test)

Example 9 with IndirectList

use of org.eclipse.persistence.indirection.IndirectList in project eclipselink by eclipse-ee4j.

the class CacheableModelJunitTest method testIndirectCollectionRefreshBehavior.

// Bug 347190
public void testIndirectCollectionRefreshBehavior() {
    QuerySQLTracker counter = new QuerySQLTracker(getServerSession());
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        CacheableFalseEntity entity = new CacheableFalseEntity();
        CacheableFalseDetailWithBackPointer detail = new CacheableFalseDetailWithBackPointer();
        List<CacheableFalseDetailWithBackPointer> details = new ArrayList<CacheableFalseDetailWithBackPointer>();
        details.add(detail);
        entity.setDetailsBackPointer(details);
        detail.setEntity(entity);
        em.persist(entity);
        em.flush();
        em.clear();
        counter.getSqlStatements().clear();
        Query query = em.createQuery("SELECT e from JPA_CACHEABLE_FALSE e where e.id = :id").setParameter("id", entity.getId());
        query.setHint(QueryHints.REFRESH, HintValues.TRUE);
        entity = (CacheableFalseEntity) query.getResultList().get(0);
        IndirectList list = (IndirectList) entity.getDetailsBackPointer();
        assertFalse(list.isInstantiated());
        assertEquals(2, counter.getSqlStatements().size());
    } finally {
        rollbackTransaction(em);
        counter.remove();
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) CacheableFalseDetailWithBackPointer(org.eclipse.persistence.testing.models.jpa.cacheable.CacheableFalseDetailWithBackPointer) ArrayList(java.util.ArrayList) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker) IndirectList(org.eclipse.persistence.indirection.IndirectList) CacheableFalseEntity(org.eclipse.persistence.testing.models.jpa.cacheable.CacheableFalseEntity) ChildCacheableFalseEntity(org.eclipse.persistence.testing.models.jpa.cacheable.ChildCacheableFalseEntity)

Example 10 with IndirectList

use of org.eclipse.persistence.indirection.IndirectList in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method buildCloneForPartObject.

/**
 * INTERNAL:
 * Require for cloning, the part must be cloned.
 * Ignore the objects, use the attribute value.
 * this is identical to the super class except that the element must be added to the new
 * aggregates collection so that the referenced objects will be cloned correctly
 */
@Override
public Object buildCloneForPartObject(Object attributeValue, Object original, CacheKey cacheKey, Object clone, AbstractSession cloningSession, Integer refreshCascade, boolean isExisting, boolean isFromSharedCache) {
    ContainerPolicy containerPolicy = getContainerPolicy();
    if (attributeValue == null) {
        return containerPolicy.containerInstance(1);
    }
    Object clonedAttributeValue = containerPolicy.containerInstance(containerPolicy.sizeFor(attributeValue));
    Object temporaryCollection = null;
    if (isSynchronizeOnMerge) {
        // I will use a temporary collection to help speed up the process
        synchronized (attributeValue) {
            temporaryCollection = containerPolicy.cloneFor(attributeValue);
        }
    } else {
        temporaryCollection = attributeValue;
    }
    for (Object valuesIterator = containerPolicy.iteratorFor(temporaryCollection); containerPolicy.hasNext(valuesIterator); ) {
        Object wrappedElement = containerPolicy.nextEntry(valuesIterator, cloningSession);
        Object originalElement = containerPolicy.unwrapIteratorResult(wrappedElement);
        // need to add to aggregate list in the case that there are related objects.
        if (cloningSession.isUnitOfWork() && ((UnitOfWorkImpl) cloningSession).isOriginalNewObject(original)) {
            ((UnitOfWorkImpl) cloningSession).addNewAggregate(originalElement);
        }
        Object cloneValue = buildElementClone(originalElement, clone, cacheKey, refreshCascade, cloningSession, isExisting, isFromSharedCache);
        Object clonedKey = containerPolicy.buildCloneForKey(containerPolicy.keyFromIterator(valuesIterator), clone, cacheKey, refreshCascade, cloningSession, isExisting, isFromSharedCache);
        containerPolicy.addInto(clonedKey, cloneValue, clonedAttributeValue, cloningSession);
    }
    if (temporaryCollection instanceof IndirectList) {
        ((IndirectList) clonedAttributeValue).setIsListOrderBrokenInDb(((IndirectList) temporaryCollection).isListOrderBrokenInDb());
    }
    return clonedAttributeValue;
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) IndirectList(org.eclipse.persistence.indirection.IndirectList)

Aggregations

IndirectList (org.eclipse.persistence.indirection.IndirectList)15 EntityManager (jakarta.persistence.EntityManager)7 ArrayList (java.util.ArrayList)7 List (java.util.List)5 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 FetchGroup (org.eclipse.persistence.queries.FetchGroup)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DirectCollectionChangeRecord (org.eclipse.persistence.internal.sessions.DirectCollectionChangeRecord)3 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)3 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)3 TreeMap (java.util.TreeMap)2 IndexedObject (org.eclipse.persistence.internal.helper.IndexedObject)2 BatchValueHolder (org.eclipse.persistence.internal.indirection.BatchValueHolder)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)2 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)2 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)2 OrderedChangeObject (org.eclipse.persistence.internal.sessions.OrderedChangeObject)2