public
Created

Method to extract parameters from Hibernate query

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
private Map getParameters(Object obj) {
Map parameters = new HashMap();
Object query = null;
if (obj instanceof AbstractQueryImpl) {
query = obj;
} else {
return parameters;
}
try {
AbstractQueryImpl q = (AbstractQueryImpl) query;
Field f = AbstractQueryImpl.class.getDeclaredField("namedParameters");
f.setAccessible(true);
Map namedParameters = (Map) f.get(q);
for (Iterator iter = namedParameters.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String name = (String) entry.getKey();
TypedValue value = (TypedValue) entry.getValue();
Object o = value.getValue();
String valueStr;
if (o instanceof Calendar) {
valueStr = DateUtils.printCal((Calendar) o);
} else {
valueStr = o.toString();
}
parameters.put(name, valueStr);
}
f = AbstractQueryImpl.class.getDeclaredField("namedParameterLists");
f.setAccessible(true);
namedParameters = (Map) f.get(q);
for (Iterator iter = namedParameters.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String name = (String) entry.getKey();
TypedValue value = (TypedValue) entry.getValue();
Object o = value.getValue();
String valueStr;
if (o instanceof Calendar) {
valueStr = DateUtils.printCal((Calendar) o);
} else {
valueStr = o.toString();
}
if (valueStr.length() > 500) {
valueStr = valueStr.substring(0, 500) + "...";
}
parameters.put(name, valueStr);
}
} catch (Throwable t) {
if (logger.isDebugEnabled()) {
logger.debug("Error intercepting query parameters", t);
}
}
return parameters;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.