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();
}
}
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());
}
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());
}
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());
}
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());
}
Aggregations