Search in sources :

Example 1 with FetchPlan

use of javax.jdo.FetchPlan 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)

Aggregations

Entry (java.util.Map.Entry)1 FetchPlan (javax.jdo.FetchPlan)1 JDOQLTypedQuery (javax.jdo.JDOQLTypedQuery)1 JDOUserException (javax.jdo.JDOUserException)1 Query (javax.jdo.Query)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)1 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)1 QueryMetaData (org.datanucleus.metadata.QueryMetaData)1