我的 HQL 代码:
select count(products.id) ,products.id, products.productDescription, p.products.productFileName
from Polls as p
group by products.id
order by count(products.id) desc
此查询返回
Coloumn 1 | Column 2 | Column 3 | Column 4
3 | 2 | Product1 | ProductFilename1
2 | 1 | Product2 | ProductFilename2
我希望将它们放在一个列表中,然后在 jsp 页面中显示它们。
我的问题是:我是否需要创建新类,以便我可以映射字段,因为仅针对此查询的计数?请问有什么帮助吗?
我的课程是:
public class Polls implements java.io.Serializable {
private Integer id;
private Products products;
private Users users;
private Date voteDate;
public Polls() {
}
public Polls(Products products, Users users, Date voteDate) {
this.products = products;
this.users = users;
this.voteDate = voteDate;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Products getProducts() {
return this.products;
}
public void setProducts(Products products) {
this.products = products;
}
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
public Date getVoteDate() {
return this.voteDate;
}
public void setVoteDate(Date voteDate) {
this.voteDate = voteDate;
}
}
产品类别:
public class Products implements java.io.Serializable {
private Integer id;
private String productDescription;
private String productFileName;
private Set pollses = new HashSet(0);
public Products() {
}
public Products(String productDescription) {
this.productDescription = productDescription;
}
public Products(String productDescription, String productFileName, Set pollses) {
this.productDescription = productDescription;
this.productFileName = productFileName;
this.pollses = pollses;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProductDescription() {
return this.productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public String getProductFileName() {
return this.productFileName;
}
public void setProductFileName(String productFileName) {
this.productFileName = productFileName;
}
public Set getPollses() {
return this.pollses;
}
public void setPollses(Set pollses) {
this.pollses = pollses;
}
}
我正在尝试使用增强的 for 循环显示列表,如下所示:
<%
List<Object[]> myGadgets = new GadgetsRepository().getGadgetDescVotes();
for (Object p : myGadgets) {%>
<div class="gadgetItem">
<div><%=p[0]%> </div>//this don't work
</div>
<% }%>