i have next tables:
Code:
Table table1
table1Id
field1
field2
field3
Table table2
table2Id
table2_field1
table2_field2
table2_field3
table1Id
in this method i get Objects from table1 sorted by some field
Code:
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;
}
then i need to get Objects from database sorted by some field, but these Objects depend on Objects from table1
Code:
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;
}
what i want to have is method like this:
Code:
public Item getTheBest(List<Long> table1Ids)
thus this method composes these two above methods and makes less calculations.
the idea of the method is to have a Collection of Objects, sorted by one criteria and after sorting by this criteria, we choose items by some field.
the
so how can i do it in hibernate?
so
Code:
public Item getTheBest(List<Long> table1Ids)
method must select data based on ids from the first table and from the selected elements would be chosen elements, chosen via sorting.