How to add a specific field in Spring Data Rest?

对着背影说爱祢 提交于 2019-12-02 11:57:24

You can use projection in the output of your repo method.

  1. So in your case you can set a projection, for example:
@Projection(name = "BookWithRating", types = { Book.class }) 
interface BookWithRating { 

  Float getRating(); 

  Book getBook(); 
}
  1. Then setup a query method:
@Query("select avg(rl.rating) as rating, b as book from ReadingList rl join rl.book b group by rl.book order by rating desc")
Page<BookWithRating> findAllWithRating(Pageable pageable);

Pay attention to the alias of the output parameters - their names must match the projection getters.

Also you can try to use this technics for enriching a data model (see how to 'annotate exposed properties with @Value using SpEL expressions to expose synthetic properties').

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!