Package jakarta.data

Record Class Limit

Record Components:
maxResults - maximum number of results for a query.
startAt - starting position for query results (1 is the first result).

public record Limit(int maxResults, long startAt) extends Record

Limits the number of results of a single invocation of a repository find method to a maximum amount or to within a positional range.

Limit is optionally specified as a parameter to a repository method in one of the parameter positions after the query parameters. For example,

 @Query("SELECT o FROM Products o WHERE o.weight <= ?1 AND o.width * o.length * o.height <= ?2 ORDER BY o.price DESC")
 Product[] freeShippingEligible(float maxWeight, float maxVolume, Limit limit);
 
 ...
 mostExpensive50 = products.freeShippingEligible(6.0f, 360.0f, Limit.of(50));
 ...
 secondMostExpensive50 = products.freeShippingEligible(6.0f, 360.0f, Limit.range(51, 100));
 

A repository method will fail if

  • multiple Limit parameters are supplied to the same method.
  • Limit and Pageable parameters are supplied to the same method.
  • a Limit parameter is supplied in combination with the First keyword.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Limit(int maxResults, long startAt)
    Limits query results.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    int
    Maximum number of results that can be returned for a single invocation of the repository method.
    static Limit
    of(int maxResults)
    Create a limit that caps the number of results at the specified maximum, starting from the first result.
    static Limit
    range(long startAt, long endAt)
    Create a limit that restricts the results to a range, beginning with the startAt position and ending after the endAt position or the position of the final result, whichever comes first.
    long
    Offset at which to start when returning query results.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Limit

      public Limit(int maxResults, long startAt)

      Limits query results. For more descriptive code, use:

      Parameters:
      maxResults - maximum number of results for a query.
      startAt - starting position for query results (1 is the first result).
  • Method Details

    • maxResults

      public int maxResults()

      Maximum number of results that can be returned for a single invocation of the repository method.

      Returns:
      maximum number of results for a query.
    • startAt

      public long startAt()

      Offset at which to start when returning query results. The first query result is position 1.

      Returns:
      offset of the first result.
    • of

      public static Limit of(int maxResults)

      Create a limit that caps the number of results at the specified maximum, starting from the first result.

      Parameters:
      maxResults - maximum number of results.
      Returns:
      limit that can be supplied to a find...By or @Query method; will never be null.
      Throws:
      IllegalArgumentException - if maxResults is less than 1.
    • range

      public static Limit range(long startAt, long endAt)

      Create a limit that restricts the results to a range, beginning with the startAt position and ending after the endAt position or the position of the final result, whichever comes first.

      Parameters:
      startAt - position at which to start including results. The first query result is position 1.
      endAt - position after which to cease including results.
      Returns:
      limit that can be supplied to a find...By or @Query method; will never be null.
      Throws:
      IllegalArgumentException - if startAt is less than 1 or endAt is less than startAt, or the range from startAt to endAt exceeds Integer.MAX_VALUE.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.