Search in sources :

Example 11 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method initialiseNamedQueries.

/**
 * Method to compile all registered named queries (when the user has initialised using a persistence-unit).
 * @param clr ClassLoader resolver
 */
protected void initialiseNamedQueries(ClassLoaderResolver clr) {
    MetaDataManager mmgr = getMetaDataManager();
    Set<String> queryNames = mmgr.getNamedQueryNames();
    if (queryNames != null) {
        // Compile all named queries of JDOQL/JPQL language using dummy ExecutionContext
        ExecutionContext ec = getExecutionContext(null, null);
        for (String queryName : queryNames) {
            QueryMetaData qmd = mmgr.getMetaDataForQuery(null, clr, queryName);
            if (qmd.getLanguage().equals(QueryLanguage.JPQL.toString()) || qmd.getLanguage().equals(QueryLanguage.JDOQL.toString())) {
                if (NucleusLogger.QUERY.isDebugEnabled()) {
                    NucleusLogger.QUERY.debug(Localiser.msg("008017", queryName, qmd.getQuery()));
                }
                org.datanucleus.store.query.Query q = storeMgr.newQuery(qmd.getLanguage().toString(), ec, qmd.getQuery());
                q.compile();
                q.closeAll();
            }
        }
        ec.close();
    }
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) MetaDataManager(org.datanucleus.metadata.MetaDataManager)

Example 12 with QueryMetaData

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

the class AnnotationTest method testNamedQuery.

/**
 * Test of JPA @NamedQuery, @NamedNativeQuery.
 */
public void testNamedQuery() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryMetaData[] qmds = cmd.getQueries();
    assertNotNull("LoginAccount has no queries!", qmds);
    assertEquals("LoginAccount has incorrect number of queries", 2, qmds.length);
    QueryMetaData jpqlQuery = null;
    QueryMetaData sqlQuery = null;
    if (qmds[0].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[1];
    }
    if (qmds[0].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[1];
    }
    if (jpqlQuery == null) {
        fail("No JPQL Query was registered for LoginAccount");
    }
    if (sqlQuery == null) {
        fail("No SQL Query was registered for LoginAccount");
    }
    assertEquals("LoginAccount JPQL has incorrect query name", "LoginForJohnSmith", jpqlQuery.getName());
    assertEquals("LoginAccount JPQL has incorrect query", "SELECT a FROM LoginAccount a WHERE a.firstName='John' AND a.lastName='Smith'", jpqlQuery.getQuery());
    assertEquals("LoginAccount SQL has incorrect query name", "LoginForJohn", sqlQuery.getName());
    assertEquals("LoginAccount SQL has incorrect query", "SELECT * FROM JPA_AN_LOGIN WHERE FIRSTNAME = 'John'", sqlQuery.getQuery());
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 13 with QueryMetaData

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

the class XMLTest method testNamedQuery.

/**
 * Test of JPA <named-query>, <named-native-query>
 */
public void testNamedQuery() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryMetaData[] qmds = cmd.getQueries();
    assertNotNull("LoginAccount has no queries!", qmds);
    assertEquals("LoginAccount has incorrect number of queries", 2, qmds.length);
    QueryMetaData jpqlQuery = null;
    QueryMetaData sqlQuery = null;
    if (qmds[0].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[1];
    }
    if (qmds[0].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[1];
    }
    if (jpqlQuery == null) {
        fail("No JPQL Query was registered for LoginAccount");
    }
    if (sqlQuery == null) {
        fail("No SQL Query was registered for LoginAccount");
    }
    assertEquals("LoginAccount JPQL has incorrect query name", "LoginForJohnSmith", jpqlQuery.getName());
    assertEquals("LoginAccount JPQL has incorrect query", "SELECT a FROM LoginAccount a WHERE a.firstName='John' AND a.lastName='Smith'", jpqlQuery.getQuery());
    assertEquals("LoginAccount SQL has incorrect query name", "LoginForJohn", sqlQuery.getName());
    assertEquals("LoginAccount SQL has incorrect query", "SELECT * FROM JPALOGIN WHERE FIRSTNAME = 'John'", sqlQuery.getQuery());
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 14 with QueryMetaData

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

the class AnnotationPlusXMLOverrideTest method testQuery.

/**
 * A named query using annotation @Query would/should use 'language="JDOQL"',
 * so it's up to the JDO implementation to translate it to applicable SQL
 * - even if it's vendor specific.
 * <p/>
 * Test named query (language="SQL"): proprietary vendor SQL in "package.orm"
 * overriding ANSI-SQL in "package.jdo".
 */
public void testQuery() {
    // Person
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    String prefix = cmd.getFullClassName() + " : ";
    QueryMetaData[] qmds = cmd.getQueries();
    assertNotNull(prefix + "query info is incorrect", qmds);
    assertEquals(prefix + "query name incorrect", "NumberOfPeople", qmds[0].getName());
    assertEquals(prefix + "query language incorrect", "SQL", qmds[0].getLanguage());
    assertFalse(prefix + "query unmodifiable incorrect", qmds[0].isUnmodifiable());
    assertTrue(prefix + "query unique incorrect", qmds[0].isUnique());
    assertEquals(prefix + "query string incorrect", "SELECT COUNT(ALL) AS THESIZE FROM PERSON", qmds[0].getQuery());
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 15 with QueryMetaData

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

the class AnnotationTest method testQueries.

/**
 * Test for use of annotations with queries
 */
public void testQueries() {
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    String prefix = cmd1.getFullClassName() + " : ";
    QueryMetaData[] qmds = cmd1.getQueries();
    assertNotNull(prefix + "query info is incorrect", qmds);
    assertEquals(prefix + "query name incorrect", "PeopleCalledSmith", qmds[0].getName());
    assertEquals(prefix + "query language incorrect", "JDOQL", qmds[0].getLanguage());
    assertFalse(prefix + "query unmodifiable incorrect", qmds[0].isUnmodifiable());
    assertFalse(prefix + "query unique incorrect", qmds[0].isUnique());
    assertEquals(prefix + "query string incorrect", "SELECT FROM org.jpox.samples.annotations.models.company.Person WHERE lastName == \"Smith\"", qmds[0].getQuery());
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Aggregations

QueryMetaData (org.datanucleus.metadata.QueryMetaData)15 ClassMetaData (org.datanucleus.metadata.ClassMetaData)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)3 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)3 MetaDataManager (org.datanucleus.metadata.MetaDataManager)3 Map (java.util.Map)2 JDOQLTypedQuery (javax.jdo.JDOQLTypedQuery)2 NucleusContext (org.datanucleus.NucleusContext)2 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)2 JDOQuery (org.datanucleus.api.jdo.JDOQuery)2 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)2 InvalidClassMetaDataException (org.datanucleus.metadata.InvalidClassMetaDataException)2 PackageMetaData (org.datanucleus.metadata.PackageMetaData)2 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)2