2

特定の企業で利用可能な場所の数をカウントするクエリを作成しました。SQLクエリは次のようになります

SELECT 
e.enterprise_id,
e.ent_name,
(
    SELECT count(l.location_id)
    FROM rfx_ent_locations l
    WHERE l.loc_type_id <> 1001
        AND e.enterprise_id = l.enterprise_id
) AS locCount
FROM rfx_enterprises e
WHERE  e.partner_type = 102;

entity の列として導入すると、列が見つからないというエラーが表示されます。エイリアスとして使用される locCount を含むエンティティとして取得するには、JPA を使用してどのように進めればよいですか?

4

1 に答える 1

0

それを使用してカウントクエリを実行しないでください。このように個別に実行してください

Query query = session.createQuery("SELECT count(l.location_id)
    FROM rfx_ent_locations l
    WHERE l.loc_type_id <> 1001
        AND e.enterprise_id = l.enterprise_id");// do as you please with the query result

    /**
     * instead of doing a second query to get the count.  Just do a quick count using Iterator
     */

    for (Iterator iterator = query.iterate(); iterator.hasNext();) {
        iterator.next();
        count++;
    }

    Integer queryCount = count;

この queryCount をクラスの@transient可変セッター メソッドに設定して、カウントを取得できるようにします。

または
このようにしてみてください

Integer count = (Integer) session.CreateQuery("SELECT count(l.location_id)
FROM rfx_ent_locations l
WHERE l.loc_type_id <> 1001
    AND e.enterprise_id = l.enterprise_id").UniqueResult();

乾杯!!!

于 2012-09-17T07:53:39.383 に答える