Search in sources :

Example 1 with AttributeItem

use of org.eclipse.persistence.internal.queries.AttributeItem in project eclipselink by eclipse-ee4j.

the class EntityGraphImpl method buildAttributeNodes.

protected void buildAttributeNodes() {
    // this instance was built from a pre-existing attribute group so we need to rebuild
    // and entity graph
    this.attributeNodes = new HashMap<String, AttributeNodeImpl>();
    for (AttributeItem item : this.attributeGroup.getItems().values()) {
        AttributeNodeImpl node = new AttributeNodeImpl(item.getAttributeName());
        ClassDescriptor localDescriptor = null;
        if (this.descriptor != null) {
            localDescriptor = this.descriptor.getMappingForAttributeName(item.getAttributeName()).getReferenceDescriptor();
        }
        if (item.getGroups() != null && !item.getGroups().isEmpty()) {
            for (AttributeGroup subGroup : item.getGroups().values()) {
                Class<?> type = subGroup.getType();
                if (type == null) {
                    type = CoreClassConstants.OBJECT;
                }
                if (localDescriptor != null) {
                    if (!type.equals(CoreClassConstants.OBJECT) && localDescriptor.hasInheritance()) {
                        localDescriptor = localDescriptor.getInheritancePolicy().getDescriptor(type);
                    }
                    node.addSubgraph(new EntityGraphImpl(subGroup, localDescriptor));
                } else {
                    node.addSubgraph(new EntityGraphImpl(subGroup));
                }
            }
        }
        if (item.getKeyGroups() != null && !item.getKeyGroups().isEmpty()) {
            for (AttributeGroup subGroup : item.getKeyGroups().values()) {
                Class<?> type = subGroup.getType();
                if (type == null) {
                    type = CoreClassConstants.OBJECT;
                }
                if (localDescriptor != null) {
                    if (!type.equals(CoreClassConstants.OBJECT) && localDescriptor.hasInheritance()) {
                        localDescriptor = localDescriptor.getInheritancePolicy().getDescriptor(type);
                    }
                    node.addKeySubgraph(new EntityGraphImpl(subGroup, localDescriptor));
                } else {
                    node.addKeySubgraph(new EntityGraphImpl(subGroup));
                }
            }
        }
        this.attributeNodes.put(item.getAttributeName(), node);
    }
}
Also used : AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup)

Example 2 with AttributeItem

use of org.eclipse.persistence.internal.queries.AttributeItem in project eclipselink by eclipse-ee4j.

the class DescriptorIterator method iteratePrimitiveForMapping.

/**
 * Iterate on the primitive value for its mapping.
 */
public void iteratePrimitiveForMapping(Object primitiveValue, DatabaseMapping mapping) {
    if (primitiveValue == null) {
        return;
    }
    setCurrentMapping(mapping);
    setCurrentDescriptor(null);
    if (shouldIterateOnPrimitives()) {
        // false by default
        AttributeGroup currentGroupOriginal = null;
        AttributeItem currentItemOriginal = null;
        if (this.usesGroup) {
            currentGroupOriginal = this.currentGroup;
            currentItemOriginal = this.currentItem;
            this.currentGroup = this.currentItem.getGroup();
        }
        internalIteratePrimitive(primitiveValue);
        if (this.usesGroup) {
            this.currentGroup = currentGroupOriginal;
            this.currentItem = currentItemOriginal;
        }
    }
}
Also used : AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup)

Example 3 with AttributeItem

use of org.eclipse.persistence.internal.queries.AttributeItem in project eclipselink by eclipse-ee4j.

the class FetchGroupManager method prepareAndVerifyInternal.

/**
 * INTERNAL:
 * Add primary key and version attributes to the passed fetch group
 * and all the fetch group it contains.
 * Also verifies that all the attributes have corresponding mappings.
 */
protected void prepareAndVerifyInternal(FetchGroup fetchGroup, String attributePrefix) {
    addMinimalFetchGroup(fetchGroup);
    ObjectBuilder builder = this.descriptor.getObjectBuilder();
    if (fetchGroup.isValidated()) {
        return;
    }
    Iterator<Map.Entry<String, AttributeItem>> it = fetchGroup.getAllItems().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, AttributeItem> entry = it.next();
        String name = entry.getKey();
        DatabaseMapping mapping = builder.getMappingForAttributeName(name);
        if (mapping != null) {
            FetchGroup nestedFetchGroup = (FetchGroup) entry.getValue().getGroup();
            if (nestedFetchGroup != null) {
                if (mapping.isForeignReferenceMapping()) {
                    ClassDescriptor referenceDescriptor = mapping.getReferenceDescriptor();
                    if (referenceDescriptor != null) {
                        FetchGroupManager nestedFetchGroupManager = referenceDescriptor.getFetchGroupManager();
                        if (nestedFetchGroupManager != null) {
                            nestedFetchGroupManager.prepareAndVerifyInternal(nestedFetchGroup, attributePrefix + name + '.');
                        } else {
                            // target descriptor does not support fetch groups
                            throw ValidationException.fetchGroupHasWrongReferenceClass(fetchGroup, name);
                        }
                    } else {
                        // no reference descriptor found
                        throw ValidationException.fetchGroupHasWrongReferenceAttribute(fetchGroup, name);
                    }
                } else if (mapping.isAggregateObjectMapping()) {
                    ClassDescriptor referenceDescriptor = mapping.getReferenceDescriptor();
                    if (referenceDescriptor != null) {
                        FetchGroupManager nestedFetchGroupManager = referenceDescriptor.getFetchGroupManager();
                        if (nestedFetchGroupManager != null) {
                            nestedFetchGroupManager.prepareAndVerifyInternal(nestedFetchGroup, attributePrefix + name + '.');
                        } else {
                            // target descriptor does not support fetch groups
                            throw ValidationException.fetchGroupHasWrongReferenceClass(fetchGroup, name);
                        }
                    } else {
                        // no reference descriptor found
                        throw ValidationException.fetchGroupHasWrongReferenceAttribute(fetchGroup, name);
                    }
                } else {
                    // no reference mapping found
                    throw ValidationException.fetchGroupHasWrongReferenceAttribute(fetchGroup, name);
                }
            }
        } else {
            // no mapping found
            throw ValidationException.fetchGroupHasUnmappedAttribute(fetchGroup, name);
        }
    }
}
Also used : AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with AttributeItem

use of org.eclipse.persistence.internal.queries.AttributeItem in project eclipselink by eclipse-ee4j.

the class SimpleSerializeFetchGroupTests method verifyWriteReplaceOnFetchGroup.

@Test
public void verifyWriteReplaceOnFetchGroup() throws Exception {
    EntityFetchGroup fg = new EntityFetchGroup(new String[] { "basic", "a" });
    // fg.addAttribute("basic");
    // fg.addAttribute("a.b");
    // assertTrue(fg.getClass() == FetchGroup.class);
    FetchGroup serFG = serialize(fg);
    assertNotNull(serFG);
    assertTrue(serFG.getClass() == EntityFetchGroup.class);
    assertTrue(serFG.hasItems());
    AttributeItem basicFI = serFG.getItem("basic");
    assertNotNull(basicFI);
    // assertTrue(basicFI instanceof DetachedFetchItem);
    AttributeItem aFI = serFG.getItem("a");
    assertNotNull(aFI);
    // assertTrue(aFI instanceof DetachedFetchItem);
    // serialized EntityFetchGroup is always flat - doesn't have nested groups.
    assertNull(aFI.getGroup());
/*assertNotNull(aFI.getGroup());
        assertTrue(aFI.getGroup() instanceof EntityFetchGroup);
        EntityFetchGroup aEFG = (EntityFetchGroup) aFI.getGroup();
        assertNull(aEFG.getParent());
        assertTrue(aEFG.hasItems());

        AttributeItem bFI = aEFG.getItem("b");

        assertNotNull(bFI);
        //assertTrue(bFI instanceof DetachedFetchItem);
        assertNull(bFI.getGroup());*/
}
Also used : EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) Test(org.junit.Test)

Example 5 with AttributeItem

use of org.eclipse.persistence.internal.queries.AttributeItem in project eclipselink by eclipse-ee4j.

the class NestedNamedFetchGroupTests method dynamicHierarchicalFetchGroup.

@Test
public void dynamicHierarchicalFetchGroup() throws Exception {
    EntityManager em = createEntityManager();
    Query query = em.createQuery("SELECT e FROM Employee e WHERE e.gender = :GENDER");
    query.setParameter("GENDER", Gender.Male);
    // Define the fields to be fetched on Employee
    FetchGroup fg = new FetchGroup();
    fg.addAttribute("firstName");
    fg.addAttribute("lastName");
    fg.addAttribute("salary");
    fg.addAttribute("gender");
    fg.addAttribute("manager.firstName");
    fg.addAttribute("manager.lastName");
    fg.addAttribute("manager.salary");
    fg.addAttribute("manager.gender");
    fg.addAttribute("manager.manager.firstName");
    fg.addAttribute("manager.manager.lastName");
    fg.addAttribute("manager.manager.salary");
    fg.addAttribute("manager.manager.gender");
    AttributeItem mgrItem = fg.getItem("manager");
    assertNotNull(mgrItem);
    assertNotNull(mgrItem.getGroup());
    AttributeItem mgrMgrItem = fg.getItem("manager.manager");
    assertNotNull(mgrMgrItem);
    assertNotNull(mgrMgrItem.getGroup());
    query.setHint(QueryHints.FETCH_GROUP, fg);
    List<Employee> emps = query.getResultList();
    int numSelect = getQuerySQLTracker(em).getTotalSQLSELECTCalls();
    List<Employee> loadedEmps = new ArrayList<Employee>();
    loadedEmps.addAll(emps);
    for (Employee emp : emps) {
        if (!loadedEmps.contains(emp)) {
            assertFetched(emp, fg);
        }
        assertNotFetchedAttribute(emp, "startDate");
        loadedEmps.add(emp);
    }
// TODO assertEquals(1 + loadedEmps.size() - emps.size(), numSelect);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) ArrayList(java.util.ArrayList) FetchGroup(org.eclipse.persistence.queries.FetchGroup) Test(org.junit.Test)

Aggregations

AttributeItem (org.eclipse.persistence.internal.queries.AttributeItem)24 FetchGroup (org.eclipse.persistence.queries.FetchGroup)14 Test (org.junit.Test)12 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)8 AttributeGroup (org.eclipse.persistence.queries.AttributeGroup)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 CoreAttributeGroup (org.eclipse.persistence.core.queries.CoreAttributeGroup)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 FetchGroupManager (org.eclipse.persistence.descriptors.FetchGroupManager)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)1 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)1