簡體   English   中英

使用休眠條件求和和按日期分組

[英]Sum and Group by Date using Hibernate Criteria

我有以下示例帳戶表:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM