hibernate标准设置投影groovy

时间:2013-11-02 07:33:51

标签: hibernate criteria hibernate-criteria nhibernate-projections

我正在使用groovy:

这很有效,但我需要它有一个字段映射因为它返回[[1,2],[2,2]]。我需要它像这样[[caseId:1,countId:2],[caseId:2,countId:2]]

def childNotes = ChildNote.withCriteria() {
            createAlias("caseInstance", "caseInstance")
            createAlias("caseInstance.caseTypeInstance", "caseTypeInstance")
            createAlias("childNoteEnteredBy", "childNoteEnteredBy")
           createAlias("assessmentTypeIdAssessmentType", "assessmentTypeIdAssessmentType")
           eq("caseTypeInstance.caseTypeDescrip", "Youth")
           eq("childNoteEnteredBy.id", ccuEmployeeId as Long)
           'in'("assessmentTypeIdAssessmentType.id", assesmentTypes)
           projections { groupProperty("caseInstance" ) 
                count("caseInstance")

               }

        }

这不起作用

 projections { groupProperty("caseInstance" ) as "caseId"
                        count("caseInstance") as "caseCount"

                       }

我也试过了createcriteria并在我的代码中使用了这个例子,但是Projections.property行中的“Projections”无法识别,即使我已经导入 org.hibernate。 criterion.ProjectionList

List results = session.createCriteria(Domestic.class, "cat")
.createAlias("kittens", "kit")
.setProjection( Projections.projectionList()
    .add( Projections.property("cat.name"), "catName" )
    .add( Projections.property("kit.name"), "kitName" )caseInstance
)
.addOrder( Order.asc("catName") )
.addOrder( Order.asc("kitName") )
.list();

有人能为我指出正确的语法吗?感谢。

1 个答案:

答案 0 :(得分:0)

读取结果的每一行并为每一行创建一个Map,或者使用AliasToEntityMapResultTransformer,这样就可以为您做到这一点:

criteria.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)