Home > database >  Can I use dynamic Join in queryDSL?
Can I use dynamic Join in queryDSL?

Time:07-19

I'm new in QueryDSL and having concern creating a QueryDSL with joins. I want to use one method and change dynamically query's join. One query joins A, B tables and another query joins A, B, C tables. I can't find these from querydsl reference. Can I do this? My code is below.

@Override
    public List<PostPreviewDto> findPostByCategoryName(String account, String categoryName, int cursor) {
        return query.select(
                Projections.constructor(PostPreviewDto.class,
                        post.id, post.title, post.time,
                        user.name, user.image, layout, like.count())
        )
                .from(post)
                .innerJoin(user).on(post.user.eq(user).and(user.account.eq(account)))
                .innerJoin(category).on(category.eq(post.category).and(category.name.eq(categoryName)))
                .innerJoin(like).on(post.eq(like.post))
                .innerJoin(mold).on(mold.eq(post.mold))
                .innerJoin(layout).on(mold.eq(layout.mold).and(layout.main.eq(true)))
                .where(greaterThanCursor(cursor))
                .groupBy(post)
                .orderBy(post.id.desc())
                .limit(PAGE_SIZE)
                .fetch();
    }

    @Override
    public List<PostPreviewDto> findPostByUserId(Long userId, int cursor) {
        return query.select(
                Projections.constructor(PostPreviewDto.class,
                        post.id, post.title, post.time,
                        user.name, user.image, layout, like.count())
        )
                .from(post)
                .innerJoin(user).on(post.user.eq(user).and(user.id.eq(userId)))
                .innerJoin(like).on(post.eq(like.post))
                .innerJoin(mold).on(mold.eq(post.mold))
                .innerJoin(layout).on(mold.eq(layout.mold).and(layout.main.eq(true)))
                .where(greaterThanCursor(cursor))
                .groupBy(post)
                .orderBy(post.id.desc())
                .limit(PAGE_SIZE)
                .fetch();
    }

CodePudding user response:

You should be able to do something like this:

private JPQLQuery<PostPreviewDto> getBaseQuery(int cursor) {
    return query.select(Projections.constructor(PostPreviewDto.class,
                                                post.id, post.title, post.time,
                                                user.name, user.image, layout, like.count())
            )
            .from(post)
            .innerJoin(user).on(post.user.eq(user))
            .innerJoin(like).on(post.eq(like.post))
            .innerJoin(mold).on(mold.eq(post.mold))
            .innerJoin(layout).on(mold.eq(layout.mold).and(layout.main.eq(true)))
            .where(greaterThanCursor(cursor))
            .groupBy(post)
            .orderBy(post.id.desc())
            .limit(PAGE_SIZE);
}

@Override
public List<PostPreviewDto> findPostByCategoryName(String account, String categoryName, int cursor) {
    return getBaseQuery(cursor)
            .innerJoin(category).on(category.eq(post.category))
            .where(user.account.eq(account).and(category.name.eq(categoryName)))
            .fetch();
}

@Override
public List<PostPreviewDto> findPostByUserId(Long userId, int cursor) {
    return getBaseQuery(cursor)
            .where(user.id.eq(userId))
            .fetch();
}
  • Related