如何将下面的sql转换为QueryDSL代码?

时间:2015-12-11 07:10:43

标签: querydsl

我正在测试QueryDSL。 如何将下面的sql转换为QueryDSL代码?

select c.*
,(
    select count(*)
    from project_category_map m
    , project p
    where 1=1
    and m.category_id = c.category_id
    and m.project_id = p.project_id
) as project_count
from category c

1 个答案:

答案 0 :(得分:0)

public List<Tuple> tuple(Category category) {
    QProjectCategoryMap qProjectCategoryMap = QProjectCategoryMap.projectCategoryMap;
    QProject qProject = QProject.qProject;

    return
        this.queryFactory
            .select(this.qCategory,
                SQLExpressions
                    .select(qProject.count())
                    .from(qProjectCategoryMap)
                    .join(qProject)
                    .where(qProjectCategoryMap.categoryId.eq(qCategory.categoryId))
                    .where(qProject.projectId.eq(qProjectCategoryMap.projectId))
            )
            .from(this.qCategory)
            .fetch();
}