Sort of..when things are handled by ORM/Spring, it has all the metadata so can take decisions but for example with native queries it doesn't have the required metadata like how the columns should be mapped as compared to when this is handled by Spring Data/Hibernate.
You can...same problem many solutions...it's just that with mapper you're still fetching all fields then remapping while with projections it's spring who's not only fetching data selectively but also remapping object. As always we should assess the usecase and measure the solution
Got you. I totally lost the point that we are fetching selective data. I'm not a spring dev, but I'm just curious, how do we manage the same with a dto (class). Let's say we are not using projection, so in that case, do we get the data as List from @query, then loop thorough and manually map to dto ? Or how it is ?
Correct. There are some methods which return Object[]..could be native query or even some method in the *Repository interface..when it happens we write mappers to traverse the array.
Also, why does interface projection won't work for custom/native quries. Spring would not do mapping in case of native query and interface ?
Sort of..when things are handled by ORM/Spring, it has all the metadata so can take decisions but for example with native queries it doesn't have the required metadata like how the columns should be mapped as compared to when this is handled by Spring Data/Hibernate.
Can't we make use of object mapper and map entities to dto ?
You can...same problem many solutions...it's just that with mapper you're still fetching all fields then remapping while with projections it's spring who's not only fetching data selectively but also remapping object. As always we should assess the usecase and measure the solution
Got you. I totally lost the point that we are fetching selective data. I'm not a spring dev, but I'm just curious, how do we manage the same with a dto (class). Let's say we are not using projection, so in that case, do we get the data as List from @query, then loop thorough and manually map to dto ? Or how it is ?
Correct. There are some methods which return Object[]..could be native query or even some method in the *Repository interface..when it happens we write mappers to traverse the array.