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

Specifies a limit on the number of results retrieved by a repository method. The results of a single invocation of a repository method may be limited to a given maximum number of results or to a given positioned range defined in terms of an offset and maximum number of results.

A query method of a repository may have a parameter of type Limit if its return type indicates that it may return multiple entities. The parameter of type Limit must occur after the method parameters representing regular parameters of the query itself. For example,

 Product[] findByNameLike(String namePattern, Limit limit, Sort<?>... sorts);
 
 ...
 mostExpensive50 = products.findByNameLike(pattern, Limit.of(50), Sort.desc("price"));
 ...
 secondMostExpensive50 = products.findByNameLike(pattern, Limit.range(51, 100), Sort.desc("price"));
 

A repository method may not be declared with:

  • more than one parameter of type Limit,
  • a parameter of type Limit and a parameter of type PageRequest, or
  • a parameter of type Limit 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 method or @Query method that performs a find operation; 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 method or or a @Query method that performs a find operation; 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.