私は非エンティティクラスを持っています
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.