Is there a way to transform objects that spring data repositories return?

后端 未结 1 1488
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 17:05

Right now I have an entity object and a DTO. The repository returns a list of objects arrays when I do a simple example like: findById(). Is there a way to easily map the re

1条回答
  •  旧时难觅i
    2020-12-30 17:41

    Try this.

    @Query("Select new package.FoodDto(f.id, f.name) from Food f where f.id = :id")
    public List findById(@Param("id") String id);
    

    Assuming class FoodDto is in package, if not you need to set the full package.

    Also I assume the FoodDto have a constructor that match

    public FoodDto(int id, String name){
     //Variable assignation
    }
    

    I never tried in spring-jpa but that works in JPQL so I assume it will work XD

    0 讨论(0)
提交回复
热议问题