3

There is a use case I am struggling with SDR as below -

THere is User Table and RefSecQuestion tables

User -> ManyTOOne -> RefSecQuestion , RefSecQuestion -> OneToMany -> User

THere is User Table and UserFriends tables

User -> OneToMany UserFriends , UserFriends -> ManyToOne -> User

There is a requirement that when I go /users/{id}/userFriends , then firstname , lastname etc from UserProjection should be shown by default

As a result, I enabled excerptProjection in UserRepository and it works fine. I expect about 100 results here so that is fine if this result is not paginated.

But , now since RefSecQuestion is also related to User , what happens is that when I go /refSecQuestions -> this page hangs since it is trying to substitute user link with UserProjection. The RefSecQuestion table is skewed with one question for most of users and therefore the page breaks due to loss of pagination.

since i cant choose unidirectionality here as both url are needed i.e

/users/{id}/userFriends
/refSecQuestions/users  

THe closest answer I found was to choose unidirectinality which is that I set Rest Export to false for User in RefSEcQuestion

fortm
  • 4,066
  • 5
  • 49
  • 79
  • You already know the answer: http://stackoverflow.com/questions/24775583/paginate-sub-resources-in-spring-data-rest-2-1/24783715 – a better oliver Dec 19 '14 at 10:42
  • @zeroflagL, yeah I have seen Oliver response and am not able to implement according to what he said. Can you provide some guideline here. I understood to configure SDR for ExposeIdOnly and thereafter don't understand how to do manually kepping URL same – fortm Dec 19 '14 at 12:13
  • I'm not quite sure, but I think by '_manually expose a resource_' he means writing a Spring MVC Controller. – a better oliver Dec 19 '14 at 13:23
  • I am using Spring Seccurity I configured it such that all /users/{id}/* URL were only exposed to user logged in with {id}. NOw since this pagination is not working out of box, and I got to now expose /profile ,/userLanguages separately, it is more difficult to devise spring security strategy since ids increase in number .. Therefore was looking for a solution where using Spring Data JPA trick could get same result with using Spring MVC – fortm Dec 19 '14 at 14:17
  • I also looked here and it seems what is suggested here is to create a custom find method however the URL then changes as it will have /search in it http://stackoverflow.com/questions/21469538/spring-data-rest-pageable-child-collection – fortm Dec 19 '14 at 14:19
  • http://stackoverflow.com/questions/27013726/spring-data-rest-how-to-retrieve-many-items-using-list-of-ids-in-one-single-cal/27041257#27041257 <- I suspect this is what is being suggested where we can first make a rest call to Embedded Resouce and get IDS and then run above query – fortm Dec 19 '14 at 18:40

1 Answers1

6

Finally I am able to get my desired results and am posting here for all SDR users. I wanted to have pagination in this URL - where User -> One To Many -> UserLanguages

/users/{id}/userLanguages

Now Using default SDR configuration, embedded resources don't get paginated and therefore I have to manually expose them and the Workaround is as below which still takes very less lines of code -

@RestController
public class MainController {

    @RequestMapping(value = "/users/{id}/userLanguages", method = RequestMethod.GET)
    @PreAuthorize("permitAll")
    public ModelAndView findUserLanguages(@PathVariable Integer id) {
        ModelAndView model = new ModelAndView("forward:/userLanguages/search/findByUserId?userId=" + id);
        return model;
    }

then, in UserLangRepository

public interface UserLanguageRepository extends BaseRepository<UserLanguage, Integer> {

    Page<UserLanguage> findByUserId(@Param("userId") Integer userId, Pageable pageable);
}

Here name findByUserId follows Spring Data Query Derivation rule where there is user column in UserLanguage and id column in User. Then following URL is paginated and have other options as well like sort, size etc..

http://localhost:8585/MYAPP/users/3/userLanguages

There is an issue however that next and prev links point to forwarded link..

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
fortm
  • 4,066
  • 5
  • 49
  • 79
  • This worked for me, I would like to find another solution if possible, but for now it solves the problem, thank you! – Hatem Jaber Mar 21 '17 at 15:53