QueryDSL projections with @ManyToOne relation

余生长醉 提交于 2019-12-04 10:06:41
Timo Westkämper

You can use nested projections for this case

List<Folder> listFolders = query.from(folder)
    .list(Projections.bean(Folder.class, folder.name, 
          Projections.bean(File.class, folder.file.fileName).as("file")));

Here is a more explicit alternative to constructor and bean projection that should also work for this case

MappingProjection<Folder> mapping = new MappingProjection<Folder>(Folder.class, folder.name, folder.file.fileName) {
 @Override
 protected Folder map(Tuple row) {
     Folder f = new Folder();         
     f.setName(row.get(folder.name));
     File file = new File();
     file.setFileName(row.get(folder.file.fileName));
     f.setFile(file);
     return f;
 }            

};

Related http://www.querydsl.com/static/querydsl/3.6.0/apidocs/com/mysema/query/types/MappingProjection.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!