0
我有下一个表:休眠标准问题
Table table1
table1Id
field1
field2
field3
Table table2
table2Id
table2_field1
table2_field2
table2_field3
table1Id
在这个方法我从表1的对象有些外地
public List<table1> getMost() {
DetachedCriteria criteria = (DetachedCriteria) DetachedCriteria.forClass(table1.class);
//criteria.add(Restrictions.conjunction());
criteria.addOrder(Order.desc("field1"));
List<table1> myList = (List<table1>) findByCriteria(criteria,
false, 0, 10);//get first 10 elements by some criteria
return myList;
}
然后我需要从数据库中获得一些字段排序对象排序,但这些对象取决于表1中的对象
public Item getTheBest(Long table1Id) {
DetachedCriteria criteria = (DetachedCriteria) DetachedCriteria
.forClass(Item.class);
DetachedCriteria maxQuery = DetachedCriteria.forClass(Item.class);
maxQuery.add(Restrictions.eq("table1Id", table1Id)).setProjection(
Projections.max("table2_field1"));
criteria.add(Restrictions.and(
Restrictions.eq("table1Id", table1Id),
Property.forName("table2_field1").eq(maxQuery)));
List<Item> result = (List<Item>) findByCriteria(criteria, false);
if (result.iterator().hasNext())
return result.iterator().next();
else
return null;
}
我想要的是这样的方法:
public Item getTheBest(List<Long> table1Ids)
因此,这种方法构成了上述两种方法,并减少了计算量。 该方法的想法是有一个对象的集合,按一个标准排序,并按照这个标准排序后,我们选择一些字段的项目。 那么我该如何做到这一点在休眠?
@疯狂怪胎。更确切地说,当我选择了一些来自第一个表的数据,我想从第二个选择数据,其中ID是相等的,并且不能有超过1个具有table1id值的代表实例 – 2012-07-10 12:58:19