2010-01-14 22 views
28

Sto cercando di ottenere un rapporto utilizzando Criteria e ProjectionList, e sono piuttosto nuovo che usa questo attraverso l'ibernazione. Così ho questo modello:Raggruppa per mese con i criteri in Hibernate

private Long _userId; 

private Category _category; 

private Long _companyId; 

private Double _amount; 

private Date _date; 

E costruire la query utilizzando questo:

public List sumPaymentsByUserCategoryPeriod(Category category, Long userId,Integer period){ 
    GregorianCalendar from = new GregorianCalendar(); 
    from.add(Calendar.MONTH, -period); 
    List<CategoryAmount> resultDTO= new ArrayList<CategoryAmount>(); 

    Criteria criteria = getSession().createCriteria(Payment.class); 
    criteria.add(Restrictions.eq("_category", category)); 
    criteria.add(Restrictions.eq("_userId",userId)); 
    criteria.add(Restrictions.between("_date", from.getTime(), new Date())); 

    ProjectionList projectionList = Projections.projectionList(); 
    projectionList.add(Projections.sum("_amount")); 
    projectionList.add(Projections.groupProperty("_date")); 
    criteria.setProjection(projectionList); 
    return criteria.list(); 

} 

Fondamentalmente questo metodo ricevono una categoria e un ID utente per filtrare i pagamenti record e un periodo, che indicherà quanti mesi da ora in poi voglio riassumere. Come posso ottenere il risultato somma raggruppato per mesi?

Qualsiasi aiuto o suggerimento lo apprezzerò!

risposta

37

Ho trovato la risposta, ed è piuttosto semplice. Ho modificato la proprietà "groupProperty" nei criteri di ProjectionList per questo:

projectionList.add(Projections.sqlGroupProjection(
    "month({alias}.DATE) as month, year({alias}.DATE) as year", 
    "month({alias}.DATE), year({alias}.DATE)", 
    new String[]{"month","year"}, 
    new Type[] {Hibernate.DOUBLE})); 

OK. Spiegherò la sqlGroupProjection. Il primo argomento è la parte della query dopo del "selezionare", ad esempio:

Select [firstPartOfSqlGroupProjection] * boo; 

il "{alias}" è l'alias hibernates uso nella query per fare riferimento a una tabella.

Il secondo argomento della funzione sqlGroupProjection è il gruppo per criteri, il terzo argomento sono i nomi di colonna che si otterranno dal gruppo e, infine, il tipo di dati che si utilizzerà.

+3

Come gestire diversi database? come MSSQL e Oracle? come Oracle usa to_char e mssql usa weeknum ... – zolibra

0

È possibile utilizzare uno SQLQuery in ibernazione, ecco un esempio:

public List<MonthlyPoint> groupByMonth(ChartRequest request){ 

    SQLQuery query = getSession().createSQLQuery("SELECT \n" + 
     "sum(this_.amount) as amount, \n" + 
     "extract(month from this_.fecha) as month, \n" + 
     "extract(year from this_.fecha) as year\n" + 
     "FROM jornada this_ \n" + 
     "GROUP BY \n" + 
     "month, \n" + 
     "year \n" + 
     "ORDER BY \n" + 
     "year asc, \n" + 
     "month asc"); 

     query.setResultTransformer(Transformers.aliasToBean(MonthlyPoint.class)); 
     return query.list(); 
} 


public class MonthlyPoint { 
    private Double year; 
    private Double month; 
    private Double amount; 
    //-- getters and setters here -- 
} 
Problemi correlati