Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

spring data - Use case for Pagination of Embedded resources

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...