Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Map Query Results to DTO in Joiner Framework with Kotlin #18

Closed
ByteByteBrew opened this issue Feb 21, 2024 · 3 comments
Closed

Comments

@ByteByteBrew
Copy link

Could you please provide guidance on how to map query results to a DTO object in the Joiner framework? I couldn't find examples in the documentation that cover this scenario. I've attempted to use Querydsl's Projections for this purpose, but I encountered issues. Is there a limitation or specific method within the Joiner framework for handling result projection, especially when Projections from Querydsl are involved? Any advice or examples for this use case would be much appreciated.

@encircled
Copy link
Owner

Sorry for the late response.

As of now, there is no support for mapping projections to a DTO, only to JPA entities. As a workaround, you may map result to a Tuple and then use custom mapping to any object- https://github.com/encircled/Joiner/tree/master?tab=readme-ov-file#result-projection

I will consider implementing a native support, as it is supported by querydsl

@encircled
Copy link
Owner

Actually is does work, here is an example:

    @Test
    public void toDto() {
        List<MyDto> dto = joiner.find(
                Q.select(Projections.constructor(MyDto.class,
                                QUser.user.id,
                                QUser.user.name))
                        .from(QUser.user)
        );
    }

    public static class MyDto {
        public Long id;
        public String name;

        public MyDto(Long id, String name) {
            this.id = id;
            this.name = name;
        }
    }

I will just add some syntax sugar for such mappings in future release

@encircled
Copy link
Owner

@ByteByteBrew released ver 1.5 with syntax like:

val dto = joinerKt.find(
            listOf(user.id, user.name)
                    mappingTo IdAndName::class
                    from user
        )

data class IdAndName(val id: Long, val name: String)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants