Search code examples
querydsl

querydsl CollectionTable joinColumns dto Projections not an entity


I got an error not an entity

I think should create a uri entity but I want to get the result using CollectionTable annotation

It works without uri in dto

How can I use the uri in dto?

expect query(example)

select gai.*, gaiu.uri from goose_auth_items gai
join goose_auth_items_uri gaiu on gai.item_identity = gaiu.goose_auth_items_id
where gai.user_identity = 1;

entity

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(
    name = "goose_auth_items_uri",
    joinColumns = @JoinColumn(name = "goose_auth_items_id")
)
@Column(name = "uri")
@Builder.Default
private List<String> uri = new ArrayList<>();

QClass

public final ListPath<String, StringPath> uri = this.<String, StringPath>createList("uri", String.class, StringPath.class, PathInits.DIRECT2);

dto

private List<String> uri = new ArrayList<>();

query

return queryFactory.select(
    Projections.constructor(GooseAuthGetItemsResponseDto.class,
        gooseAuthItems.name,
        gooseAuthItems.userName,
        gooseAuthItems.userPassword,
        gooseAuthItems.folder,
        gooseAuthItems.notes,
        gooseAuthItems.uri, <-- not working
        gooseAuthItems.updateDate)
    ).from(gooseAuthItems)
    .where(gooseAuthItems.userMaster.eq(userMaster))
    .fetch();

I wanted to use Projections.constructor to get only I needed I want to uri in the json response body as a list


Solution

  • This is just a limitation of JPA, you cannot project plural relationships in a tuple. You have to join them. Then afterwards you have to group the results to combine the various uri elements for each entity.