在我的DAO层中,我有一个像这样的Find函数
public List> findCategoryWithSentenceNumber(int offset, int maxRec) { Criteria crit = getSession().createCriteria(Category.class, "cate"); crit.createAlias("cate.sentences", "sent"); crit.setProjection(Projections.projectionList(). add(Projections.property("title"), "title"). add(Projections.count("sent.id"), "numberOfSentence"). add(Projections.groupProperty("title")) ); crit.setFirstResult(offset); crit.setMaxResults(maxRec); return crit.list(); }
所以,为了读取数据,我必须使用一个循环(带Iterator
)
List> result = categoryDAO.findCategoryWithSentenceNumber(0, 10); // ListdtoList = new ArrayList<>(); for (Iterator> it = result.iterator(); it.hasNext(); ) { Object[] myResult = (Object[]) it.next(); String title = (String) myResult[0]; Long count = (Long) myResult[1]; assertEquals("test", title); assertEquals(1, count.intValue()); // dQCategoryDTO = new DQCategoryDTO(); // dQCategoryDTO.setTitle(title); // dQCategoryDTO.setNumberOfSentence(count); // dtoList.add(dQCategoryDTO); }
我的问题是:是否有任何api,框架可以轻松地将List> result
in 转换为DTO
对象列表(例如,DQCategoryDTO)而不使用任何循环,迭代器和调用setter/getter来填充值?