Return custom object from Spring Data with Native Query

后端 未结 5 2132
一生所求
一生所求 2020-12-10 04:18

My question is based on another post. How can I achieve the same with a native query? Native queries do not allow JPQL thus do not allow new instances either.

My POJ

5条回答
  •  误落风尘
    2020-12-10 04:52

    If you are using a recent version of spring-data and also making use of the Repositories, I personally think that the answer from Itsallas leads to the right solution.

    I actually did't now about (Spring Data) Projections yet and needed a moment to understand what he was showing in his example.

    Therefore I just want to add a link to the Spring Data JPA - Reference Documentation, have a look at the Projections chapter.

    Spring Data query methods usually return one or multiple instances of the aggregate root managed by the repository. However, it might sometimes be desirable to create projections based on certain attributes of those types. Spring Data allows modeling dedicated return types, to more selectively retrieve partial views of the managed aggregates.

提交回复
热议问题