2010-01-14 18 views
28

Tôi đang cố gắng để có được một báo cáo bằng cách sử dụng tiêu chí và ProjectionList, và tôi là khá mới bằng cách sử dụng này thông qua ngủ đông. Vì vậy, tôi có mô hình này:Nhóm theo tháng với tiêu chí trong Hibernate

private Long _userId; 

private Category _category; 

private Long _companyId; 

private Double _amount; 

private Date _date; 

Và tôi xây dựng các truy vấn sử dụng này:

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

} 

Về cơ bản phương pháp này nhận được một loại và một userId để lọc các khoản thanh toán sổ sách và một thời gian, những người sẽ chỉ ra bao nhiêu tháng kể từ bây giờ trở lại tôi muốn tổng hợp. Làm thế nào tôi có thể nhận được kết quả tổng hợp theo tháng?

Bất kỳ trợ giúp hoặc mẹo nào tôi sẽ đánh giá cao!

Trả lời

37

Tôi đã tìm thấy câu trả lời và nó khá đơn giản. Tôi đã thay đổi "groupProperty" trong tiêu chí ProjectionList cho điều này:

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. Tôi sẽ giải thích sqlGroupProjection. Đối số đầu tiên là một phần của các truy vấn sau của "chọn", ví dụ:

Select [firstPartOfSqlGroupProjection] * boo; 

các "{alias}" là bí danh mà ngủ đông sử dụng trong truy vấn để tham khảo một bảng.

Đối số thứ hai của hàm sqlGroupProjection là nhóm theo tiêu chí, đối số thứ ba là tên cột mà bạn sẽ nhận được từ nhóm theo và cuối cùng là loại dữ liệu bạn sẽ sử dụng.

+3

Làm thế nào để xử lý DB khác nhau? chẳng hạn như MSSQL và Oracle? khi Oracle sử dụng to_char và mssql use weeknum ... – zolibra

0

Bạn có thể sử dụng một sqlquery trong ngủ đông, đây là một ví dụ:

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 -- 
}