我有以下示例帐户表:
id amount amountDate
1 2.2 2016-09-01
2 4.4 2016-09-02
... ... ...
31 1.1 2016-10-01
32 2.2 2016-10-2我想用amountDate来和amount列组,以得到结果:
Amount Amount_Month
6.6 2016-September
3.3 2016-October(此结果必须显示在primefaces中)
这就是我要尝试的:
在AccountDAO.java中,我有这两个函数:
public Double getTotalAmmount(){
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Account.class);
cr.setProjection(Projections.sum("ammount"));
List total = cr.list();
session.close();
return (Double)total.get(0);
}
public List getD()
{
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Account.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.sqlGroupProjection("date(ammountDate) as amD", "amD", new String[] { "amD" }, new Type[] { StandardBasicTypes.DATE }));
criteria.setProjection(projectionList);
return criteria.list();
}在AccountBean.java中
private Double totalAmmount;
private List<Account> total;
//with their setter and getter
@PostConstruct
public void onCreate(){
loadAccounts();
}
public void loadAccounts(){
total = AccountDAO.getInstance().getD();
totalAmmount = AccountDAO.getInstance().getTotalAmmount();
}这是primefaces:
<p:panel id="list" header="Accounts">
<h:form id="actores">
<p:dataTable
value="#{accountBean.totalAmmount}"
var="account"
id="dataTable"
paginator="true"
rows="12">
<p:column>
<f:facet name="header">Sum_of_Ammount</f:facet>
<h:outputText value="#{accountBean.totalAmmount}" />
</p:column>
<p:column>
<f:facet name="header">Months</f:facet>
<h:outputText value="#{accountBean.total}" />
</p:column>
</p:dataTable>
</h:form>
</p:panel>但其结果是:
Sum_of_Ammount Months
59.20000000000002 [[Ljava.lang.Object;@2093e746,
[Ljava.lang.Object;@30890fe4, ...发布于 2016-11-13 09:41:11
您要将列表返回到客户端视图。因此,它是不正确的显示。
private Double totalAmmount;
private List<Account> total;
//with their setter and getter
@PostConstruct
public void onCreate(){
loadAccounts();
}
public void loadAccounts(){
total = AccountDAO.getInstance().getD(); // this is List.
totalAmmount = AccountDAO.getInstance().getTotalAmmount();
}https://stackoverflow.com/questions/40572393
复制相似问题