Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringData Page support #32

Open
b0c1 opened this issue Jul 19, 2016 · 7 comments
Open

SpringData Page support #32

b0c1 opened this issue Jul 19, 2016 · 7 comments

Comments

@b0c1
Copy link

b0c1 commented Jul 19, 2016

Hi!

There is any way to get the Query object (before the run but after the fillQueryParameters) ? I want to call with Spring Data Pageable object to get paged result. Or JINQ have another way to create spring boot Page object? (It need the total number of result, not just the "page")

@my2iu
Copy link
Owner

my2iu commented Jul 19, 2016

Give me a few days to look over this Spring Data Pageable and see what's possible. While it is certainly possible to tweak JINQ so that you can get at the underlying Query object, you would then need to process the result set yourself, so things would start looking a little untidy. I'll try to see if something better can be done.

@my2iu
Copy link
Owner

my2iu commented Jul 20, 2016

It seems like the best choice might be to create a Jinq method for returning a Page from a Pageable. I'm still trying to figure out how it works. It seems like Spring simply issues two queries: one to get the total number of results and another to get the page data. It might take a while because I'll need to make a Spring project that actually uses Pageable so that I can do testing.

@my2iu
Copy link
Owner

my2iu commented Jul 20, 2016

Unfortunately, it looks like it'll take me a couple of days to figure out how to make a Spring project that uses pagination. I don't have those sorts of cycles available at the moment, so I'm going to have to defer this for a while. If you can find a sample project that uses the Pageable stuff (preferably, a variant of the Spring Petclinic), then I can probably work with that, but otherwise, I don't have the time at the moment to dig through the Spring documentation to see how to set up server-side pagination properly.

@b0c1
Copy link
Author

b0c1 commented Jul 20, 2016

Yeah, I found same things. Spring will use two query.
My solution:

protected <C, T> Page<C> asPaged(JPAJinqStream<T> source, Pageable pageable) {
        long total = source.count();
        if (pageable != null && pageable.getPageSize() > 0) {
            source = source.skip(pageable.getPageNumber() * pageable.getPageSize())
                    .limit(pageable.getPageSize());
        }
        List<C> result = source.toList();
        return new PageImpl<>(result, pageable, total);
}

@my2iu
Copy link
Owner

my2iu commented Jul 21, 2016

Great! Since you have a workaround, I won't prioritize making a solution to this issue. The solution I was thinking of is basically the same as what you have, but since it would take me a long to build the proper infrastructure to test it, it's probably not worth the trouble unless a lot of people need the same thing.

@b0c1
Copy link
Author

b0c1 commented Aug 1, 2016

Yeah but I have a problem with this... in spring the sorting is defined in the Pageable object... but it's string based... how can I convert it to CollectComparable ?

@my2iu
Copy link
Owner

my2iu commented Aug 1, 2016

I'm not sure. As I mentioned before, I'm having difficulty creating a simple Spring example that uses a Pageable that I can use to dig into the problem.

Presumably, you could just look at the string, and then manually write code to sort the results by the appropriate field. It's not a general mechanism, but if you're using Spring already, then you're probably having Spring autogenerate most of your queries and are only using Jinq for a couple of hard ones, in which case, you would only need to do it a few times.

I don't actually know what's in the string either. If you're willing to do the sort on the application server, then you could maybe use reflection to find a method/field that corresponds to the string and then sort the objects by that field. It's not clear where the string comes from either--does Spring verify that the String is valid, or can the user hack up different parameters to change it, leading to potential security problems?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants