use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class CriteriaQueryTestSuite method testCriteriaDeleteCompareSQL.
public void testCriteriaDeleteCompareSQL() {
EntityManager em = createEntityManager();
JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("DELETE FROM PhoneNumber phone where phone.owner.firstName is not null");
String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
List baseSQLStrings = query.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
// test query "Delete Employee e where e.firstName is not null";
CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
CriteriaDelete<PhoneNumber> cq = qb.createCriteriaDelete(PhoneNumber.class);
Root<PhoneNumber> root = cq.from(PhoneNumber.class);
cq.where(qb.isNotNull(root.get("owner").get("firstName")));
EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
List testSQLStrings = testQuery.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
closeEntityManager(em);
if (testSQL != null) {
assertEquals("Delete Criteria query did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\" but we expected: \"" + baseSQL + "\"", testSQL, baseSQL);
} else {
// check list of strings instead
boolean pass = true;
if (testSQLStrings == null || baseSQLStrings == null || testSQLStrings.size() != baseSQLStrings.size()) {
pass = false;
} else {
List clonedBaseStrings = new java.util.ArrayList(baseSQLStrings);
for (String testSQLString : (List<String>) testSQLStrings) {
if (clonedBaseStrings.contains(testSQLString)) {
clonedBaseStrings.remove(testSQLString);
} else {
pass = false;
break;
}
}
if (clonedBaseStrings.size() != 0) {
pass = false;
}
}
assertTrue("Delete Criteria query translated strings did not match JPQL query; criteria generated " + testSQLStrings + " but JPQL generated " + baseSQLStrings, pass);
}
}
use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class AbstractQueryResource method namedQueryInternal.
/**
* Executes given named query.
*
* @param version the version
* @param persistenceUnit the persistence unit
* @param queryName named query to execute
* @param headers the http headers
* @param uriInfo the uri info
* @return the response
*/
protected Response namedQueryInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "namedQueryInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
try {
final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
final Query query = context.buildQuery(getMatrixParameters(uriInfo, persistenceUnit), queryName, getMatrixParameters(uriInfo, queryName), getQueryParameters(uriInfo));
final DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
final FeatureSet featureSet = context.getSupportedFeatureSet();
final Response response;
if (featureSet.isSupported(Feature.PAGING)) {
response = processPageableQuery(context, queryName, dbQuery, query, headers, uriInfo);
} else {
response = namedQueryResponse(context, queryName, dbQuery, query, headers, uriInfo, featureSet.getResponseBuilder(Feature.NO_PAGING));
}
return response;
} catch (Exception ex) {
throw JPARSException.exceptionOccurred(ex);
}
}
Aggregations