0

私は非エンティティクラスを持っています

public class CountryStatistics {

    public CountryStatistics(Long numTowns, Long numVillages) {
    ...
    }

}

特定の国について、統計オブジェクトを構築したいと考えています。

国、村、市がエンティティなので、以下のコードに沿って試してみました。

String queryString = 
"SELECT NEW mypackage.CountryStatistics(count(t), count(v)) 
 FROM Town t, Village v WHERE t.country = :country AND v.country = :country"

TypedQuery<CountryStatistics> query = 
em.createQuery(queryString ,CountryStatistics.class).setParameter("country", country);

query.getSingleResult()

質問: 同じクエリで異なるテーブルのいくつかのエンティティをカウントする正しい方法は何ですか?

上記のクエリでは、以下のように個別に配置すると、最終的に大きな数字になり、町の数は正しくなります。

"SELECT NEW mypackage.CountryStatistics(count(distinct t), count(v)) 
 FROM Town t, Village v WHERE t.country = :country AND v.country = :country"

しかし、村にも設定すると、次のようになります。

java.sql.SQLSyntaxErrorException: Multiple DISTINCT aggregates are not supported at this time.
4

1 に答える 1

2

次のクエリ(テストされていません)は、あなたが望むことをするはずです:

select count(distinct t.id), count(distinct v.id) from Country c
left join c.towns t
left join c.villages v
where c.id = :countryId
于 2013-03-23T13:57:48.017 に答える