在数据表中显示查询(select 计数分组依据)
Display query (select count group by) in a dataTable
这个查询在控制台上工作正常,但现在我想在 dataTable
的 xhtml
页面中显示它,我试过这个 attemp
但它没有工作
List<Object[]> results;
public List<Object[]> getResults() {
results = entityManager.createQuery("select d.lib_naturepanne,count(r) as nbrr"+" from NaturePanne d JOIN d.listReparation r GROUP BY d.lib_naturepanne").getResultList();
for (Object[] result : results) {
String name = (String) result[0];
int count = ((Number) result[1]).intValue();
System.out.println("name "+name+ " count "+count);
}
return results;
file.xhtml
<rich:dataTable id="tablereparation" value="#{stat.results}" var="rep">
<h:column headerClass="headerleftfacet">
<f:facet name="header">Name</f:facet>
<h:outputText value="#{rep.results.name}" /></h:column>
<h:column headerClass="headermiddlefacet">
<f:facet name="header">Count</f:facet>
<h:outputText value="#{rep.results.count}" /></h:column>
</rich:dataTable>
#{rep.results.name}
是错误的。你有一个 List<Object[]>
,所以你要么得到这样的结果:#{rep[0]}
,要么你将你的 Object[] 映射到某种 POJO。
public class NameCount {
String name;
int count;
public NameCount(Object[] o) {
name = (String) o[0];
count = (Integer) o[1];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public List<NameCount> getResults() {
List<Object[]> results = entityManager.createQuery(...);
List<NameCount> rowset = new ArrayList<NameCount>();
for (Object[] o : results) {
rowset.add(new NameCount(o));
}
return rowset;
}
这个查询在控制台上工作正常,但现在我想在 dataTable
的 xhtml
页面中显示它,我试过这个 attemp
但它没有工作
List<Object[]> results;
public List<Object[]> getResults() {
results = entityManager.createQuery("select d.lib_naturepanne,count(r) as nbrr"+" from NaturePanne d JOIN d.listReparation r GROUP BY d.lib_naturepanne").getResultList();
for (Object[] result : results) {
String name = (String) result[0];
int count = ((Number) result[1]).intValue();
System.out.println("name "+name+ " count "+count);
}
return results;
file.xhtml
<rich:dataTable id="tablereparation" value="#{stat.results}" var="rep">
<h:column headerClass="headerleftfacet">
<f:facet name="header">Name</f:facet>
<h:outputText value="#{rep.results.name}" /></h:column>
<h:column headerClass="headermiddlefacet">
<f:facet name="header">Count</f:facet>
<h:outputText value="#{rep.results.count}" /></h:column>
</rich:dataTable>
#{rep.results.name}
是错误的。你有一个 List<Object[]>
,所以你要么得到这样的结果:#{rep[0]}
,要么你将你的 Object[] 映射到某种 POJO。
public class NameCount {
String name;
int count;
public NameCount(Object[] o) {
name = (String) o[0];
count = (Integer) o[1];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public List<NameCount> getResults() {
List<Object[]> results = entityManager.createQuery(...);
List<NameCount> rowset = new ArrayList<NameCount>();
for (Object[] o : results) {
rowset.add(new NameCount(o));
}
return rowset;
}