New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

io.github.halfweight:spring-data-jpa-cursor-paging

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io.github.halfweight:spring-data-jpa-cursor-paging

Cursor pagination jpa

  • 2.1.1
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

Spring data jpa cursor paging

CircleCI Maven Central License

The spring-data-jpa-cursor-paging Java library allows to easily implement cursor pagination for Spring Boot projects. This is a community-based project, not maintained by the Spring Framework Contributors (Pivotal)

Cursor pagination

Usage

  1. Add the following dependency

    <groupId>io.github.halfweight</groupId>
    <artifactId>spring-data-jpa-cursor-paging</artifactId>
    <version>last-version</version>
    
  2. Configure the repository base class to be used to create repository proxies for this particular configuration

    @EnableJpaRepositories(repositoryBaseClass = CustomRepositoryImpl.class)
    
  3. Create a jpa repository that extends the CustomRepository

    public interface UserRepository extends CustomRepository<User, Long> {
    
    }
    
  4. Use the created repository and call findAllBy to execute cursor pagination.

    The first parameter is a specification to filter for.

    The second parameter is a CursorPageRequest. For the first query, you have to fill the size and sort fields.

    From the second query onwards make sure to provide the continuationToken

     userRepository.findAllBy(null, CursorPageRequest.of(1, Sort.by(Sort.Order.desc("id"))))
    

    This query returns a CursorPaginationSlice that contains

    fielddescription
    contentlist of found rows
    hasNextif there are other elements
    continuationTokentoken to use to execute the next query. Keep in mind that it changes for every query
    sizesize of content

    You can use every field in your entity to sort, but keep in mind that :

    • When you use the continuationToken you have to use always the same sort fields

    • Be sure to use always a unique field as a sorting field for your query as the last parameter (for example the column id)

Customization

To convert values to strings and vice-versa the library uses the ConvertUtils from it.halfweight.spring.cursor.pagination.jpa.util. This is useful to store information into the continuationToken.

If in your entity you are using a not mapped type you would use to sort, you can add a custom converter in this way:

ConverterUtil.config(Map.of(Instant.class,new AbstractConverter() {

@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
     return (T) Instant.ofEpochMilli(Long.parseLong((String) value));
     }

@Override
protected String convertToString(Object value) throws Throwable {
     return Long.toString(((Instant)value).toEpochMilli());
     }

@Override
protected Class<?> getDefaultType() {
     return Instant.class;
 }
}));

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

FAQs

Package last updated on 19 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc