Search in sources :

Example 6 with FetchGroupMetaData

use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method newNamedQuery.

/**
 * Construct a query instance with the candidate class and the query name.
 * @param cls The class to query
 * @param queryName Name of the query.
 * @return The query
 * @param <T> Candidate type for the query
 */
public <T> Query<T> newNamedQuery(Class<T> cls, String queryName) {
    assertIsOpen();
    // Throw exception on incomplete input
    if (queryName == null) {
        throw new JDOUserException(Localiser.msg("011005", null, cls));
    }
    // Find the Query for the specified class
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    QueryMetaData qmd = ec.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
    if (qmd == null) {
        throw new JDOUserException(Localiser.msg("011005", queryName, cls));
    }
    // Create the Query
    Query query = newQuery(qmd.getLanguage(), qmd.getQuery());
    if (cls != null) {
        query.setClass(cls);
        if (!ec.getStoreManager().managesClass(cls.getName())) {
            // Load the candidate class since not yet managed
            ec.getStoreManager().manageClasses(clr, cls.getName());
        }
    }
    // Optional args that should only be used with SQL
    if (qmd.getLanguage().equals(QueryLanguage.JDOQL.toString()) && (qmd.isUnique() || qmd.getResultClass() != null)) {
        throw new JDOUserException(Localiser.msg("011007", queryName));
    }
    if (qmd.isUnique()) {
        query.setUnique(true);
    }
    if (qmd.getResultClass() != null) {
        // Set the result class, allowing for it being in the same package as the candidate
        Class resultCls = null;
        try {
            resultCls = clr.classForName(qmd.getResultClass());
        } catch (ClassNotResolvedException cnre) {
            if (cls != null) {
                try {
                    String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
                    resultCls = clr.classForName(resultClassName);
                } catch (ClassNotResolvedException cnre2) {
                    throw new JDOUserException(Localiser.msg("011008", queryName, qmd.getResultClass()));
                }
            }
        }
        query.setResultClass(resultCls);
    }
    // Add any extensions
    Map<String, String> extmds = qmd.getExtensions();
    if (extmds != null) {
        Iterator<Entry<String, String>> entryIter = extmds.entrySet().iterator();
        while (entryIter.hasNext()) {
            Entry<String, String> entry = entryIter.next();
            query.addExtension(entry.getKey(), entry.getValue());
        }
    }
    if (qmd.isUnmodifiable()) {
        query.setUnmodifiable();
    }
    if (qmd.getFetchPlanName() != null) {
        // Apply any named FetchPlan to the query
        FetchPlanMetaData fpmd = ec.getMetaDataManager().getMetaDataForFetchPlan(qmd.getFetchPlanName());
        if (fpmd != null) {
            org.datanucleus.FetchPlan fp = new org.datanucleus.FetchPlan(ec, clr);
            fp.removeGroup(org.datanucleus.FetchPlan.DEFAULT);
            FetchGroupMetaData[] fgmds = fpmd.getFetchGroupMetaData();
            for (FetchGroupMetaData fgmd : fgmds) {
                fp.addGroup(fgmd.getName());
            }
            fp.setMaxFetchDepth(fpmd.getMaxFetchDepth());
            fp.setFetchSize(fpmd.getFetchSize());
            ((JDOQuery) query).getInternalQuery().setFetchPlan(fp);
        }
    }
    return query;
}
Also used : JDOQLTypedQuery(javax.jdo.JDOQLTypedQuery) Query(javax.jdo.Query) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) FetchPlanMetaData(org.datanucleus.metadata.FetchPlanMetaData) FetchPlan(javax.jdo.FetchPlan) JDOUserException(javax.jdo.JDOUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) QueryMetaData(org.datanucleus.metadata.QueryMetaData) Entry(java.util.Map.Entry) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 7 with FetchGroupMetaData

use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.

the class TypeMetadataImpl method getFetchGroups.

public FetchGroupMetadata[] getFetchGroups() {
    Set<FetchGroupMetaData> internalFgmds = getInternal().getFetchGroupMetaData();
    if (internalFgmds == null) {
        return null;
    }
    FetchGroupMetadataImpl[] fgmds = new FetchGroupMetadataImpl[internalFgmds.size()];
    int i = 0;
    for (FetchGroupMetaData fgmd : internalFgmds) {
        fgmds[i] = new FetchGroupMetadataImpl(fgmd);
        fgmds[i].parent = this;
        i++;
    }
    return fgmds;
}
Also used : FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 8 with FetchGroupMetaData

use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.

the class FetchPlanMetadataImpl method newFetchGroupMetadata.

/* (non-Javadoc)
     * @see javax.jdo.metadata.FetchPlanMetadata#newFetchGroupMetadata(java.lang.String)
     */
public FetchGroupMetadata newFetchGroupMetadata(String name) {
    FetchGroupMetaData internalFgmd = getInternal().newFetchGroupMetaData(name);
    FetchGroupMetadataImpl fgmd = new FetchGroupMetadataImpl(internalFgmd);
    fgmd.parent = this;
    return fgmd;
}
Also used : FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 9 with FetchGroupMetaData

use of org.datanucleus.metadata.FetchGroupMetaData in project tests by datanucleus.

the class AnnotationTest method testFetchGroups.

/**
 * Test for use of annotations with fetch groups
 */
public void testFetchGroups() {
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    String prefix = cmd1.getFullClassName() + " : ";
    Set<FetchGroupMetaData> fgmds = cmd1.getFetchGroupMetaData();
    assertNotNull(prefix + "fetch group info is incorrect", fgmds);
    FetchGroupMetaData fgmd = fgmds.iterator().next();
    assertEquals(prefix + "fetch group name incorrect", "names", fgmd.getName());
    assertFalse(prefix + "fetch group fields incorrect", fgmd.getPostLoad());
    Set<FetchGroupMemberMetaData> fgmmds = fgmd.getMembers();
    assertNotNull(fgmmds);
    assertEquals(prefix + "number of fetch group fields is wrong", 2, fgmmds.size());
    Iterator<FetchGroupMemberMetaData> fgmmdIter = fgmmds.iterator();
    FetchGroupMemberMetaData fgmmd0 = fgmmdIter.next();
    FetchGroupMemberMetaData fgmmd1 = fgmmdIter.next();
    assertTrue(prefix + "fields in fetch group are incorrect", (fgmmd0.getName().equals("firstName") && fgmmd1.getName().equals("lastName")) || (fgmmd1.getName().equals("firstName") && fgmmd0.getName().equals("lastName")));
}
Also used : FetchGroupMemberMetaData(org.datanucleus.metadata.FetchGroupMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 10 with FetchGroupMetaData

use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHelper method getXMLForMetaData.

public String getXMLForMetaData(FetchGroupMetaData fgmd, String prefix, String indent) {
    StringBuilder str = new StringBuilder();
    str.append(prefix).append("<fetch-group name=\"" + fgmd.getName() + "\">\n");
    // Add fetch-groups
    Set<FetchGroupMetaData> fetchGroups = fgmd.getFetchGroups();
    if (fetchGroups != null) {
        for (FetchGroupMetaData subfgmd : fetchGroups) {
            str.append(getXMLForMetaData(subfgmd, prefix + indent, indent));
        }
    }
    // Add fields
    Set<FetchGroupMemberMetaData> members = fgmd.getMembers();
    if (members != null) {
        for (FetchGroupMemberMetaData fgmmd : members) {
            str.append(getXMLForMetaData(fgmmd, prefix + indent, indent));
        }
    }
    str.append(prefix + "</fetch-group>\n");
    return str.toString();
}
Also used : FetchGroupMemberMetaData(org.datanucleus.metadata.FetchGroupMemberMetaData) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Aggregations

FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)15 FetchGroupMemberMetaData (org.datanucleus.metadata.FetchGroupMemberMetaData)8 HashSet (java.util.HashSet)3 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)3 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)3 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 IndexMetaData (org.datanucleus.metadata.IndexMetaData)3 JoinMetaData (org.datanucleus.metadata.JoinMetaData)3 QueryMetaData (org.datanucleus.metadata.QueryMetaData)3 BitSet (java.util.BitSet)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ClassMetaData (org.datanucleus.metadata.ClassMetaData)2 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 IdentityMetaData (org.datanucleus.metadata.IdentityMetaData)2 InheritanceMetaData (org.datanucleus.metadata.InheritanceMetaData)2 InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)2