I need to retrieve items from database and show them on browser in a specific order.
I have two forEach loops, first one loops through products of factory object and second one loops through different types of each product.
I need to show ordered results based on price of each type of product but using the following code every time that I refresh the page results will be shown in different order.
I need to have the results sorted, it does not matter if get sorted while retrieving them using Hibernate or while showing on browser.
<c:forEach var="product" items="${factory.products}">
<table border="2">
<thead><tr><td>ID</td><td>Type</td><td>Amount</td></tr></thead>
<tbody>
<c:forEach var="ptype" items="${product.types}">
<tr>
<td>${ptype.id}</td>
<td>${ptype.name}</td>
<td>${ptype.price}</td>
</tr>
</c:forEach>
</tbody>
</table>
<hr/>
</c:forEach>
I am retrieving the results as following
Factory factory = (Factory) session.createCriteria(factory.class, "fac")
.createAlias("fac.products", "pro")
.createAlias("pro.types", "types")
.setFetchMode("pro", FetchMode.SELECT)
.add(Restrictions.eq("fac.id", facId))
.addOrder(Property.forName("types.price").asc())
.list().get(0);