1
我需要从数据库检索项目并以特定顺序在浏览器上显示它们。如何对数据库结果进行排序?
我有两个forEach循环,第一个循环通过工厂对象的产品,第二个循环通过不同类型的每个产品。
我需要根据每种产品的价格显示有序结果,但每次刷新页面结果时都会使用以下代码,并以不同的顺序显示。
我需要将结果排序,如果在使用Hibernate检索或在浏览器上显示时进行排序并不重要。
<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>
我检索结果如下
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);
将您解释一下这句话的顺序,我不明白这一点(顺便说一句行动呼吁救....) – J888
我怎么能知道你没拿到?问一个问题,可能是[this](http://stackoverflow.com/a/17107828/573032)或[that](http://stackoverflow.com/a/18462744/573032)或[这些]( http://stackoverflow.com/q/17244036/573032)更好地解释你。 –