Projections.sqlGroupProjection(...)
以下の基準とともに を使用する
List<CoalesceDemo> list = session.createCriteria(CoalesceDemo.class).
setProjection(Projections.projectionList()
.add(Projections.sum("sub"))
.add(Projections.sqlGroupProjection("nvl2(val, 'Y', 'N') as decodedVal", "nvl2(val, 'Y', 'N')",
new String [] {"decodedVal"}, new Type[]{BooleanType.INSTANCE}))
.add(Projections.groupProperty("name"))
.add(Projections.groupProperty("val"))
.add(Projections.groupProperty("nbr"))
.add(Projections.groupProperty("proj"))
.add(Projections.groupProperty("loc")))
.add(Restrictions.eq("name", "value")).list();
あなたが探している次のクエリになりました:
select this_.name as y0_, this_.loc as y1_, this_.nbr as y2_, this_.proj as y3_, sum(this_.sub) as y4_,
nvl2(val, 'Y', 'N') as decodedVal, this_.name as y6_, this_.val as y7_, this_.nbr as y8_,
this_.proj as y9_, this_.loc as y10_ from CoalesceTable this_ where this_.name=?
group by nvl2(val, 'Y', 'N'), this_.name, this_.val, this_.nbr, this_.proj, this_.loc
もう 1 つの方法は@Formula
、エンティティの新しいフィールドで注釈を使用することです。
私の場合、このフィールドをCoalesceDemo
クラスに追加しました
@Formula("nvl2(val, 'Y', 'N')")
public String decodedVal;
そして、以下のように Criteria クエリでこれを使用します。
List<CoalesceDemo> list = session.createCriteria(CoalesceDemo.class).
setProjection(Projections.projectionList()
.add(Projections.sum("sub"))
.add(Projections.groupProperty("name"))
.add(Projections.groupProperty("val"))
.add(Projections.groupProperty("nbr"))
.add(Projections.groupProperty("proj"))
.add(Projections.groupProperty("decodedVal"))
.add(Projections.groupProperty("loc")))
.add(Restrictions.eq("name", "value")).list();
これにより、次のようなクエリが生成されます。
select this_.name as y0_, this_.loc as y1_, this_.nbr as y2_, this_.proj as y3_, sum(this_.sub) as y4_,
this_.name as y5_, this_.val as y6_, this_.nbr as y7_, this_.proj as y8_,
nvl2(this_.val, 'Y', 'N') as y9_, this_.loc as y10_ from CoalesceTable this_ where this_.name=?
group by this_.name, this_.val, this_.nbr, this_.proj, nvl2(this_.val, 'Y', 'N'), this_.loc
これが役立つかどうかを確認し、データに対してテストすることをお勧めします。