Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
Offset of type Long in Java Object Relational Mapping Frameworks

Offset of type Long in Java Object Relational Mapping Frameworks

Posted on April 23, 2025May 10, 2025 by Toma Velev

To this day amazes me that all Java Object Relational Mapping Frameworks use 32bit Integer as parameter to the offset you could pass to the database.

  1. Spring’s Pageable uses int internally — so it cannot handle offsets beyond Integer.MAX_VALUE.
  2. (JPA API also expects int for offset and limit. https://docs.oracle.com/javaee/5/api/javax/persistence/Query.html#setFirstResult(int)
  3. Hibrnate https://docs.jboss.org/hibernate/orm/6.6/javadocs/org/hibernate/query/Page.html#page(int,int)

🔍 Breakdown:

1. Pageable uses int page and size

PageRequest.of(int page, int size)
  • The page number and size are both int, so the maximum offset is:
int maxOffset = Integer.MAX_VALUE = 2_147_483_647
  • Trying to go beyond that — e.g., page = 2_147_483_648 / size — will cause an IllegalArgumentException or overflow.

2. JPA / Hibernate API also uses int:

When you do:

query.setFirstResult(offset);
query.setMaxResults(limit);

These methods accept int parameters. So even if you somehow calculated a larger long offset, JPA will truncate or fail.

🛠️ Workarounds

✅ A. Use keyset pagination (a.k.a. “seek” pagination)

Instead of relying on offset-based pagination (which becomes slow anyway with large datasets), you paginate based on a unique key, like an id.

Example:

@Query("SELECT o FROM Order o WHERE o.id > :lastId ORDER BY o.id ASC")
List<Order> findNextPage(@Param("lastId") Long lastId, Pageable pageable);
  • This avoids the use of OFFSET entirely.
  • Much faster on large datasets.
  • Works safely even if the dataset has billions of rows.

✅ B. Use native queries with OFFSET manually

If you’re controlling the query directly and absolutely must use an offset > Integer.MAX_VALUE, you can write a native query and pass the OFFSET as a String:

@Query(value = "SELECT * FROM big_table OFFSET :offset LIMIT :limit", nativeQuery = true)
List<Entity> findPage(@Param("offset") String offset, @Param("limit") int limit);
  • This is hacky and might require tuning your JDBC driver or use of PreparedStatement.setLong().
  • Not guaranteed to work across all JPA providers or drivers.

🚨 UX

  • Avoid using huge offsets
  • Switch to keyset pagination or use time/id filtering for large data ranges.

Yes, I know it is uncommon and it falls out of a Good User Experience to go to a page two million and beyond. But, the mere need of SQL Native items somehow breaks the value of the ORMs.

  • Example of GridView Builder in Flutter
  • How to Visualize Listview inside Listview in Flutter
  • What other usages you know about public private cryptography
  • Get a Flutter App to Production
  • Firebase Dynamic Links Deprecation – migrating out to Java

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (201)
  • Graphical User Interface (13)
  • Marketing (113)
  • Software Development (268)
  • Spring (41)
  • StartUp (21)
  • Uncategorized (15)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Example of GridView Builder in Flutter
  • How to Visualize Listview inside Listview in Flutter
  • What other usages you know about public private cryptography
  • Get a Flutter App to Production
  • Firebase Dynamic Links Deprecation – migrating out to Java

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (201)
  • Graphical User Interface (13)
  • Marketing (113)
  • Software Development (268)
  • Spring (41)
  • StartUp (21)
  • Uncategorized (15)
  • Uncategorized (4)
  • Vaadin (14)