是否可以使用hibernate投影通过Group属性获取List(特定列)?

时间:2015-04-07 15:05:37

标签: mysql hibernate projection hibernate-criteria

我有一个关键要求就是这样。

文件表

---------------------------------------------------------------------------------------
  id  |      root_path      |     folder_name     |      file_name     |    user_id
---------------------------------------------------------------------------------------
   1  |    common_rootpath  |       folder 1      |       file 1       |        x
   2  |    common_rootpath  |       folder 1      |       file 2       |        x
   3  |    common_rootpath  |       folder 1      |       file 3       |        x
   4  |    common_rootpath  |       folder 1      |       file 4       |        x
   5  |    common_rootpath  |       folder 2      |       file 1       |        x
   6  |    common_rootpath  |       folder 2      |       file 2       |        x
   7  |    common_rootpath  |       folder 2      |       file 3       |        x
   8  |    common_rootpath  |       folder 3      |       file 1       |        x
   9  |    common_rootpath  |       folder 3      |       file 2       |        x

我想获得以下输出

 List ( ( folder_name = folder 1, List( file_name = file1,file2,file3,file4 ) ) ,
        ( folder_name = folder 2, List( file_name = file1,file2,file3 ) ) ,
        ( folder_name = folder 3, List( file_name = file1,file2) ) ,
      )

我知道如果关系是一对多,这很容易实现。但如果值在同一个表中,是否可能?我尝试使用以下Criteria,但正如预期的那样,它给了我IllegalArgumentException ..

Criteria cr = getSession().createCriteria(Document.class,"document")
            .createAlias("document.user", "user")
            .setProjection(Projections.projectionList()
                    .add(Projections.property("fileName"),"fileNames")
                    .add(Projections.property("folderName"),"folderName")
                    .add(Projections.groupProperty("folderName")))
                    .add(Restrictions.eq("user.id", id))
                    .setResultTransformer(Transformers.aliasToBean(Document.class));
    return cr.list();

堆栈跟踪:

org.springframework.orm.hibernate4.HibernateSystemException: IllegalArgumentException occurred while calling setter for property [com.site.model.Document.fileNames (expected type = java.util.List)]; target = [com.site.model.Document@59eeb2e4], property value = [myfilename] setter of com.site.model.Document.fileNames; nested exception is IllegalArgumentException occurred while calling setter for property [com.site.model.Document.fileNames (expected type = java.util.List)]; target = [com.site.model.Document@59eeb2e4], property value = [myfilename]

FileNames是文档模型类中的List。

0 个答案:

没有答案