use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project cuba by cuba-platform.
the class QueryImpl method preExecute.
protected void preExecute(JpaQuery jpaQuery) {
// copying behaviour of org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery()
DatabaseQuery elDbQuery = ((EJBQueryImpl) jpaQuery).getDatabaseQueryInternal();
boolean isObjectLevelReadQuery = elDbQuery.isObjectLevelReadQuery();
if (jpaQuery.getFlushMode() == FlushModeType.AUTO && (!isObjectLevelReadQuery || !((ObjectLevelReadQuery) elDbQuery).isReadOnly())) {
// flush is expected
entityChangedEventManager.beforeFlush(support.getInstances(entityManager));
support.processFlush(entityManager, true);
}
}
use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class PersistenceContext method buildQuery.
/**
* Builds the query.
*
* @param tenantId the tenant id
* @param name the name
* @param parameters the parameters
* @param hints the hints
* @return the query
*/
public Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) {
EntityManager em = getEmf().createEntityManager(tenantId);
Query query = em.createNamedQuery(name);
DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
if (parameters != null) {
Iterator<?> i = parameters.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
String key = (String) entry.getKey();
Class<?> parameterClass = null;
int index = dbQuery.getArguments().indexOf(key);
if (index >= 0) {
parameterClass = dbQuery.getArgumentTypes().get(index);
}
Object parameter = entry.getValue();
if (parameterClass != null) {
parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
}
query.setParameter(key, parameter);
}
}
if (hints != null) {
for (Map.Entry<String, ?> entry : hints.entrySet()) {
query.setHint(entry.getKey(), entry.getValue());
}
}
return query;
}
use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class PersistenceContext method constructQuery.
protected Query constructQuery(EntityManager em, String name, Map<?, ?> parameters, Map<String, ?> hints) {
Query query = em.createNamedQuery(name);
DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
if (parameters != null) {
Iterator<?> i = parameters.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
String key = (String) entry.getKey();
Class<?> parameterClass = null;
int index = dbQuery.getArguments().indexOf(key);
if (index >= 0) {
parameterClass = dbQuery.getArgumentTypes().get(index);
}
Object parameter = entry.getValue();
if (parameterClass != null) {
parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
}
query.setParameter(key, parameter);
}
}
if (hints != null) {
for (Map.Entry<String, ?> entry : hints.entrySet()) {
query.setHint(entry.getKey(), entry.getValue());
}
}
return query;
}
use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class AbstractSingleResultQueryResource method namedQuerySingleResultInternal.
/**
* Named query single result.
*
* @param version the version
* @param persistenceUnit the persistence unit
* @param queryName the query name
* @param headers the http headers
* @param uriInfo the uriInfo
* @return the response
*/
@SuppressWarnings("rawtypes")
protected Response namedQuerySingleResultInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "namedQuerySingleResultInternal", 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 FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
if (dbQuery instanceof ReportQuery) {
final List<ReportItem> reportItems = ((ReportQuery) dbQuery).getItems();
final Object queryResults = query.getSingleResult();
final Object response = responseBuilder.buildSingleResultQueryResponse(context, getQueryParameters(uriInfo), queryResults, reportItems, uriInfo);
if (response != null && response instanceof SingleResultQuery) {
final SingleResultQuery singleResultQuery = (SingleResultQuery) response;
final List<JAXBElement<?>> item = singleResultQuery.getFields();
if ((item != null) && (item.size() == 1)) {
// Fix for Bug 393320 - JPA-RS: Respect the Accept Header for a singleResultQuery
// If there is only one item in the select clause and if value of that item is binary, we will create a response with
// that binary data without converting its to Base64.
JAXBElement element = item.get(0);
Object elementValue = element.getValue();
if (elementValue instanceof byte[]) {
List<MediaType> acceptableMediaTypes = headers.getAcceptableMediaTypes();
if (acceptableMediaTypes.contains(MediaType.APPLICATION_OCTET_STREAM_TYPE)) {
return Response.ok(new StreamingOutputMarshaller(context, elementValue, headers.getAcceptableMediaTypes())).build();
}
}
}
return Response.ok(new StreamingOutputMarshaller(context, response, headers.getAcceptableMediaTypes())).build();
} else {
// something went wrong with the descriptors, return error
throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
}
}
final Object queryResult = query.getSingleResult();
return Response.ok(new StreamingOutputMarshaller(context, responseBuilder.buildSingleEntityResponse(context, getQueryParameters(uriInfo), queryResult, uriInfo), headers.getAcceptableMediaTypes())).build();
} catch (Exception ex) {
throw JPARSException.exceptionOccurred(ex);
}
}
use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.
the class CriteriaQueryTestSuite method testOnClauseCompareSQL.
/**
* This test verifies that the SQL generated for a criteria query with joins and an on clause matches
* the SQL generated for the equivalent JPQL query, to ensure that excess table joins are not occurring that do
* not affect the results.
*/
public void testOnClauseCompareSQL() {
EntityManager em = createEntityManager();
JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("Select e from Employee e left join e.manager m left join m.address a on a.city = 'Ottawa' " + "where a.postalCode is not null");
String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
Root<Employee> root = cq.from(Employee.class);
Join<Object, Object> address = root.join("manager", JoinType.LEFT).join("address", JoinType.LEFT);
address.on(qb.equal(address.get("city"), "Ottawa"));
cq.where(qb.isNotNull(address.get("postalCode")));
EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
closeEntityManager(em);
if (!testSQL.equals(baseSQL)) {
fail("Criteria query using ON clause did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\" but we expected: \"" + baseSQL + "\"");
}
}
Aggregations