Spring Data JPA Projections: Best Practices and Examples

Поділитися
Вставка
  • Опубліковано 12 гру 2024

КОМЕНТАРІ • 6

  • @universal4334
    @universal4334 4 місяці тому

    Also, why does interface projection won't work for custom/native quries. Spring would not do mapping in case of native query and interface ?

    • @the_codealchemist
      @the_codealchemist  4 місяці тому

      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.

  • @universal4334
    @universal4334 4 місяці тому

    Can't we make use of object mapper and map entities to dto ?

    • @the_codealchemist
      @the_codealchemist  4 місяці тому

      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

    • @universal4334
      @universal4334 4 місяці тому

      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 ?

    • @the_codealchemist
      @the_codealchemist  4 місяці тому

      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.