Maurice
Maurice

Reputation: 7401

Querydsl always creates a list with one child entity with null values when the parent entity has no child entities

I have the following code

public Optional<ThreadPageDTO> getThreadPageById(Long threadId, Long threadViewerId) {
    QThread thread = QThread.thread;
    QThreadLike like = QThreadLike.threadLike;
    QFlag flag = QFlag.flag;
    QImage image = QImage.image;

    JPQLQuery<Thread> threadQuery = getThreadJPQLQueryWithJoins(threadId, threadViewerId, thread, image, like, flag);

    Map<Long, ThreadPageDTO> result = threadQuery.transform(
            groupBy(thread.id).as(
                    Projections.constructor(
                            ThreadPageDTO.class,
                            thread.title,
                            thread.content,
                            thread.views,
                            thread.contentLanguage().id,
                            thread.author().id,
                            list(
                                    Projections.constructor(
                                            JournalEntryDetailsImageDTO.class,
                                            image.thumbnailImageUrl,
                                            image.previewImageUrl,
                                            image.fullImageUrl,
                                            image.id
                                    )
                            ),
                            thread.flagCount,
                            thread.likeTotal,
                            threadViewerId != null ? like.id.isNotNull() : Expressions.constant(false),
                            threadViewerId != null ? flag.id.isNotNull() : Expressions.constant(false),
                            thread.dateCreated
                    )
            )
    );

    return Optional.ofNullable(result.get(threadId));
}

When a thread entity has no related images, then QueryDSL will still add one JournalEntryDetailsImageDTO projection in list form to the returned thread. This projection will have a null value for every field.

Why does queryDSL do this and is there a way to make it return an empty list instead?

Upvotes: 0

Views: 15

Answers (0)

Related Questions