-
Notifications
You must be signed in to change notification settings - Fork 350
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
Result mapping of projections should always use the DTO and not the domain entity #1554
Comments
When Spring Data introduced projections, we used the underlying domain entity as the backing data structure and then copy over matching properties into the DTO (or apply an interface projection proxy). For some Spring Data modules (MongoDB, Cassandra), we started migrating. The main reasons for that approach have been that:
In Mongo and Cassandra modules we introduced multi-level projections ( I think we can apply a similar approach in JDBC and R2DBC modules, hence moving this ticket into Spring Data Relational. |
TL;DR: Could the result mapping strategy for DTO projections be changed to always use the DTO and not the domain entity? That would save us a lot of error prone and repetitive work, because in Kotlin null-safety prohibits instantiating the domain entities first, if not all non-null fields are also selected in the query.
Details
From the docs I get that result mapping of DTO projections basically consists of these two facts:
Effectively this means that the instantiated domain entity sets all fields
null
that were not queried. This works in Java, but not in Kotlin, because of null-safety.For example:
If there is an avatar for
userId
1L
the queryfindByUserIdIn(setOf(1L))
fails withFrom the docs I understand that to make that work it would require me to write a
@Query
annotation, because then the result mapping uses the DTO immediately:Obviously, that is error prone and unnecessary work. For instance, a major issue in this example is that the query would fail if
ids
is empty. Hence, the caller side must first guarantee thatids
is non empty - which is cumbersome. Hence, it would require us to provide a custom implementation for that simple query to return ifids
is empty.The text was updated successfully, but these errors were encountered: