Skip to content

Instantly share code, notes, and snippets.

@johndemic
Created January 14, 2013 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndemic/4532336 to your computer and use it in GitHub Desktop.
Save johndemic/4532336 to your computer and use it in GitHub Desktop.
/**
* Execute a JPA query. If the payload of the message is a <code>List</code> or <code>Map</code> then its used
* as the query parameters. If neither statement nor namedQuery are set then the payload of the message is assumed
* to be a <code>CriteriaQuery</code> instance.
* <p/>
* {@sample.xml ../../../doc/JPAModule-connector.xml.sample jpa:query}
*
* @param statement a JPA QL statement to execute
* @param namedQuery a named query to execute
* @param queryParameters the query parameters
* @param criteria the CriteriaQuery
* @return The query results
* @throws Exception An exception when there's an error
*/
@Processor
public Object query(@Optional CriteriaQuery criteria, @Optional String statement, @Optional String namedQuery,
@Optional Object queryParameters)
throws Exception {
if (logger.isDebugEnabled()) {
if (StringUtils.isNotBlank(statement)) {
logger.debug(String.format("Performing query with statement %s and parameters %s", statement,
queryParameters));
} else if (StringUtils.isNotBlank(namedQuery)) {
logger.debug(String.format("Performing query with named query %s and parameters %s", statement,
queryParameters));
} else {
logger.debug("Attempting criteria query with payload: " + criteria);
}
}
Map<String, Object> parameterMap = new HashMap<String, Object>();
if (StringUtils.isNotBlank(statement)) {
parameterMap.put("statement", statement);
}
if (StringUtils.isNotBlank(namedQuery)) {
parameterMap.put("namedQuery", statement);
}
if (namedQuery != null) {
parameterMap.put("namedQuery", namedQuery);
}
parameterMap.put("queryParameters", queryParameters);
return perform(criteria, new Query(), parameterMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment