Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest Projections with or without explicit Declaration in Repository

What is difference between creating a SDR Projection and then

(i) Explicitly declaring that in corresponding Repository as in -

@RepositoryRestResource(excerptProjection = UserSummaryProjection.class)
public interface UserRepository extends JpaRepository<User, Integer> { ..
}

(ii) Not explicitly defining Projection in Repository

What I find is that when Projection is explicitly declared in Repository , then that projection is shown for that entity wherever findAll or findByID gets called.

But when it is not declared, then there is an option left to either user / not use them so, by default all fields get shown.

some examples -

I also find that when a projection is defined, then the link disappears. For example - There is UserLanguage and RefLanguage and say, there are 2 repositories UserLanguageRepository and ResfLanguageRepository , then there is also RefLangSummaryProjection

UserLanguage -> ManytoOne -> REfLanguage RefLanguage -> OneToMany -> UserLanguage

So, going to URL /userLanguages or /userLanguages/{id} shows embedded data for RefLanguage with field coming out from RefLangSummaryProjection and link to RefLang is gone. That is probably expected however I dont like the query that is generated for REFLangSummaryProejction which still selects all columns

like image 226
fortm Avatar asked Mar 17 '23 14:03

fortm


1 Answers

Projections are basically a two fold thing:

  1. You create projections to enable clients to see different views of a resource. The projections are defined as interfaces, named and then exposed to the client by a request parameter. The client is now able to choose exactly one of the registered projections or free to choose none of them at all.

  2. You can elevate one of the registered projections to become an excerpt projection (hence the attribute name). If declared, it will automatically be used in places where related resources appear in the representation. With the default HAL format this is the case for everything that's rendered in the _embedded clause.

like image 83
Oliver Drotbohm Avatar answered Apr 27 '23 11:04

Oliver Drotbohm