3

HQL を使用していくつかの行を月ごとにグループ化しようとしていますが、その API は初めてで、うまく動作しないようです。

これが私のコードです:

        Criteria query = getHibernateSession().createCriteria(SalesPerformance.class);

        // summary report is grouped by date
        query.setProjection(Projections.projectionList().add(
                Projections.groupProperty("effectiveDate"), "effectiveDate").add(
                Projections.groupProperty("primaryKey.seller", "seller").add(
                Projections.sum("totalSales"))));


        // sub-select based on seller id
        query.add(Property.forName("primaryKey.seller.id").eq(sellerId)).setFetchMode(
                "primaryKey.seller", FetchMode.SELECT);

        query.add(Property.forName("primaryKey.effectiveDate").le(new Date()));
        query.add(Property.forName("primaryKey.effectiveDate").ge(DateUtils.truncate(new Date(), Calendar.MONTH)));
        query.addOrder(Order.desc("primaryKey.effectiveDate"));

        return query.list();

このクエリに関する私の問題は、Projections.groupProperty("effectiveDate") のために、1 か月に 1 行が必要なときに、1 日 1 行を返すことです。

Projections.groupProperty の代わりに Projections.sqlGroupProjection を使用して、いくつかの HQL を投入することを考えましたが、ドキュメントと私が見つけたいくつかの例は、そのメソッドに正しい postresql ステートメントを配置する方法を理解するのに実際には役立ちませんでした。

Postgres と HQL について知っている人は、ここでヒントを教えてください。

4

1 に答える 1

6

解決策を見つけました:

Criteria query = getHibernateSession().createCriteria(SalesPerformance.class);

    // summary report is grouped by date
            query.setProjection(Projections.projectionList().add(Projections.sqlGroupProjection("date_trunc('month', eff_dt) as eff_dt_value", "eff_dt_value", new String[] {"eff_dt_value"}, new Type[] {Hibernate.DATE})).add(
                            Projections.groupProperty("primaryKey.seller", "seller").add(
                            Projections.sum("totalSales"))));


            // sub-select based on seller id
            query.add(Property.forName("primaryKey.seller.id").eq(sellerId)).setFetchMode(
                            "primaryKey.seller", FetchMode.SELECT);

            query.add(Property.forName("primaryKey.effectiveDate").le(new Date()));
            Date beginningOfLastMonth = DateUtils.truncate(DateUtils.addMonths(new Date(), -1) , Calendar.MONTH);
            Date endOfLastMonth = DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.MONTH), -1);
            query.add(Property.forName("primaryKey.effectiveDate").between(beginningOfLastMonth, endOfLastMonth));


            return query.list();

私の場合、先月の effectiveDate で値を取得する必要があることに注意してください。

うまくいけば、それは同じボートに乗っている他の人を助けるでしょう! :)

于 2009-06-01T13:39:18.060 に答える