我有以下代码:
PersonDao.java
@Repository
@Transactional
public class PersonDao implements PersonDaoIface {
Object property;
String order;
@Autowired
private SessionFactory sessionFactory;
public PersonDao() {
}
public PersonDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
@Override
public List<Person> getAll(long first, long count) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
this.setPaging(criteria, first, count);
addSort(criteria);
return criteria.list();
}
@Override
public long getAllCount() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class)
.setProjection(Projections.rowCount());
Long i = (Long) criteria.uniqueResult();
return i;
}
@Override
public List<Person> getByFilter(Person person, int first, int count) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
criteria.add(Restrictions.eq("firstName", person.getFirstName()));
criteria.add(Restrictions.eq("lastName", person.getLastName()));
this.setPaging(criteria, first, count);
return criteria.list();
}
@Override
public long getByFilterCount(Person person) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
criteria.add(Restrictions.eq("firstName", person.getFirstName()));
criteria.add(Restrictions.eq("lastName", person.getLastName()));
criteria.setProjection(Projections.rowCount());
Long result = (Long) criteria.uniqueResult();
return result;
}
private void setPaging(Criteria criteria, long first, long count) {
criteria.setFirstResult((int) first);
criteria.setMaxResults((int) count);
}
private void addSort(Criteria criteria) {
if (property != null) {
if (order.equalsIgnoreCase(SortOrder.ASCENDING.toString())) {
criteria.addOrder(Order.asc((String)property));
} else {
criteria.addOrder(Order.desc((String)property));
}
}
}
@Override
public void setSort(Object property, String order) {
this.property = property;
this.order = order;
}
}
SortableDataProvider
public class PersonSortableDataProvider extends SortableDataProvider {
private transient PersonDaoIface personDao;
public PersonSortableDataProvider(PersonDaoIface personDao) {
this.personDao = personDao;
}
public PersonSortableDataProvider() {
}
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Iterator<Person> iterator(long first, long count) {
System.out.println(getSort());
return personDao.getAll(first, count).iterator();
}
@Override
public long size() {
long result = personDao.getAllCount();
return result;
}
@Override
public IModel<Person> model(final Object object) {
return new AbstractReadOnlyModel<Person>() {
@Override
public Person getObject() {
return (Person) object;
}
};
}
}
包含使用可排序数据提供程序的数据表的面板
public DataDisplayPanel(String id) {
super(id);
List<IColumn> columns = new ArrayList<IColumn>();
columns.add(new PropertyColumn(new Model<String>("First Name"), "firstName"));
columns.add(new PropertyColumn(new Model<String>("Last Name"), "lastName"));
AjaxFallbackDefaultDataTable table = new AjaxFallbackDefaultDataTable("personData", columns,
personSortableDataProvider, 8);
table.addTopToolbar(new HeadersToolbar(table, personSortableDataProvider));
add(table);
}
我的分页没有问题,但我无法理解如何使用hibernate进行排序,我可以看到你如何从java方面进行排序,但考虑到我可能会获得大量数据集不喜欢这个主意。
鉴于上面的代码,任何人都有办法获取数据表,单击名字或姓氏,然后使用附加的order by子句在迭代器中找到相同的查询。
答案 0 :(得分:0)
答案 1 :(得分:0)
对于遇到这种情况的任何人,我都有以下设置:
hibernate 4,spring 4和wicket 6
我使用Spring注入,如果你在SortableDataProvider中注入,它似乎是wicket和spring混淆。
我不知道究竟发生了什么;当我跨过项目时我会有一个更好的想法,但是当我将Dao类从可排序数据提供程序移出并进入页面并将其注入那里时,看起来setSort没有正确设置,然后将dao实例传递给可排序数据提供者排序正常工作。