动态生成hibernate查询的方法
public static String generateConditionQury(Class aClass,
String returnProperty, String[] propertyName,
Object[] propertyValue, String orderProperty, boolean isDescending) {
if (propertyName != null && propertyValue != null) {
if (propertyName.length != propertyValue.length)
throw new RuntimeException(
"Null or Un-mached property name-value pair");
}
StringBuffer sb = new StringBuffer();
try {
if (CommonUtils.isEmpty(returnProperty)) {
sb.append("select al from ");
} else {
if (aClass.newInstance() instanceof CampusAbstractModel) {
sb.append("select al.").append(returnProperty).append(
",al.modifiedGroup ");
sb.append(" from ");
} else {
sb.append("select al.").append(returnProperty).append(
" from ");
}
}
sb.append(aClass.getName()).append(" as").append(" al");
if (propertyName != null && propertyValue != null) {
for (int i = 0; i < propertyValue.length; i++) {
if (i == 0) {
sb.append(" where ");
} else {
sb.append(" and ");
}
sb.append(" al.").append(propertyName[i]);
if (propertyValue[i] == null) {
sb.append(" is null ");
} else {
sb.append(" = ? ");
}
}
}
sb.append(" order by");
if (orderProperty != null) {
sb.append(" al.").append(orderProperty);
if (isDescending) {
sb.append(" desc");
}
} else {
sb.append(" al.id");
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return sb.toString();
}
答案 0 :(得分:0)
**Optimized way to generate hql query dynamically**
public static String generateQueryUtilQuery(Class aClass,
String[] returnProperty, String[] propertyNames,
Object[] propertyValues, String orderProperty,
boolean isDescending, boolean isLike, boolean isDistinct) {
if (propertyNames != null && propertyValues != null) {
if (propertyNames.length != propertyValues.length)
throw new RuntimeException(
"Null or Un-mached property name-value pair");
}
StringBuffer sb = null;
if (isDistinct) {
sb = new StringBuffer("select distinct ");
} else {
sb = new StringBuffer("select ");
}
try {
for (int i = 0; i < returnProperty.length; i++) {
if (i != 0) {
sb.append(",");
}
sb.append("al.").append(returnProperty[i]);
}
if (aClass.newInstance() instanceof CampusAbstractModel) {
sb.append(", al.modifiedGroup ");
}
sb.append(" from ");
sb.append(aClass.getName()).append(" as al ");
if (propertyNames != null) {
for (int i = 0; i < propertyNames.length; i++) {
if (i == 0) {
sb.append(" where ");
} else {
sb.append(" and ");
}
if (isLike && propertyValues != null
&& propertyValues[i] instanceof String) {
sb.append(" upper(").append(" al.");
sb.append(propertyNames[i]).append(")");
sb.append(" like ? ");
} else if (isLike
&& (propertyValues == null || propertyValues[i] == null)) {
sb.append(" al.").append(propertyNames[i]);
sb.append(" like '%'");
} else {
sb.append(" al.").append(propertyNames[i]);
if (propertyValues == null || propertyValues[i] == null) {
sb.append(" is null ");
} else {
sb.append(" = ? ");
}
}
}
}
if (!CommonUtils.isEmpty(orderProperty)) {
sb.append(" order by ");
sb.append("al.").append(orderProperty);
if (isDescending) {
sb.append(" desc ");
}
} else if (!isDistinct) {
sb.append(" order by ");
sb.append(" al.id ");
if (isDescending) {
sb.append(" desc ");
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return sb.toString();
}