2016-11-13 68 views
0

我有以下示例帳戶表:點心和集團按日期

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_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, ... 

回答

0

您正在將列表返回到客戶端視圖。因此它顯示不正確。

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(); 
}