Filter { public void addOrderBy(Class<?> clazz, String field, boolean ascending) { if (field == null) { return; } Field f; Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field); assert (pair != null) : "Can't find field " + field + " in " + clazz.getName(); clazz = pair.first(); f = pair.second(); Column column = f.getAnnotation(Column.class); String name = column != null ? column.name() : field; StringBuilder order = new StringBuilder(); if (column == null || column.table() == null || column.table().length() == 0) { order.append(DbUtil.getTableName(clazz)); } else { order.append(column.table()); } order.append(".").append(name).append(ascending ? " ASC " : " DESC "); if (_orderBy == null) { _orderBy = order.insert(0, " ORDER BY ").toString(); } else { _orderBy = order.insert(0, _orderBy + ", ").toString(); } } Filter(Class<?> clazz, String field, boolean ascending, Long offset, Long limit);  Filter(long limit);  Filter(Filter that); void addOrderBy(Class<?> clazz, String field, boolean ascending); String getOrderBy(); void setOffset(Long offset); Long getOffset(); Long getLimit(); void setLimit(Long limit);  }
@Test public void testAddOrderBy() { Filter filter = new Filter(DbTestVO.class, "fieldString", true, 1L, 1L); Assert.assertTrue(filter.getOrderBy().trim().toLowerCase().equals("order by test.fld_string asc")); filter.addOrderBy(DbTestVO.class, "fieldLong", true); Assert.assertTrue(filter.getOrderBy().contains(",")); Assert.assertTrue(filter.getOrderBy().split(",")[1].trim().toLowerCase().equals("test.fld_long asc")); filter.addOrderBy(DbTestVO.class, "fieldInt", true); Assert.assertTrue(filter.getOrderBy().split(",").length == 3); Assert.assertTrue(filter.getOrderBy().split(",")[2].trim().toLowerCase().equals("test.fld_int asc")); }