Interface Query

All Known Subinterfaces:
RelationQuery
All Known Implementing Classes:
AbstractHibernateQueryAdapter, AbstractOrderableQuery, AbstractRelationQueryAdapter, EmptyQuery, HibernateQueryAdapter, ToManyRelationQueryAdapter, ToOneRelationQueryAdapter

public interface Query
Configure and execute queries.
  • Method Details

    • setParameter

      void setParameter(String name, Object value) throws PersistException
      Set a parameter for the query.
      Parameters:
      name - The name of the parameter
      value - The value of the parameter in the correct data type.
      Throws:
      PersistException - If the parameter cannot be set (invalid name, invalid value)
    • getOrdering

      Ordering getOrdering()
      Get a reference to the Ordering policy of this query. If there currently is no ordering policy, one will be created.
      Returns:
      The ordering policy of this query.
    • hasOrdering

      boolean hasOrdering()
      Check whether this query has an ordering policy already.
      Returns:
      true, if this query has an ordering policy with at least one rule, false if none or one without rules (which may mean that getOrdering() will make one up on the fly).
    • withOrder

      Query withOrder(Ordering order)
      Replace the Ordering policy of this query with the given ordering policy. If the given ordering policy is null or empty, it will be ignored and the existing ordering policy of the query will be preserved.
      Returns:
      this query.
    • prependOrder

      Query prependOrder(Ordering order)
      Prepend the given Ordering policy to the existing ordering policy of this query.
      Parameters:
      order - the ordering policy to prepend.
      Returns:
      this query.
    • appendOrder

      Query appendOrder(Ordering order)
      Append the given Ordering policy to the existing ordering policy of this query.
      Parameters:
      order - the ordering policy to append.
      Returns:
      this query.
    • getKeys

      default PrimaryKey[] getKeys()
      Returns pks with no limit
      Returns:
      PKs of result entities.
    • getKeys

      default PrimaryKey[] getKeys(Pagination pagination)
      Returns pks within given pagination
      Parameters:
      pagination - Limit and offset of result
      Returns:
      PKs of result entities.
    • execute

      EntityList execute() throws PersistException
      Execute the query in Query.Mode.LAZY mode using the default limit.
      Returns:
      The result of the query. To further process it use the EntityLists class.
      Throws:
      PersistException - If an error occurs
    • execute

      EntityList execute(Query.Mode mode) throws PersistException
      Execute the query in the specified Query.Mode using the default limit.
      Parameters:
      mode - The query mode.
      Returns:
      The result of the query.
      Throws:
      PersistException - If an error occurs
    • execute

      @Deprecated EntityList execute(int limit)
      Deprecated.
      Execute the query using the specified limit in Query.Mode.LAZY mode.
      Parameters:
      limit - The maximum number of results, see class header (really).
      Returns:
      The result of the query.
      Throws:
      PersistException - If an error occurred.
    • execute

      @Deprecated EntityList execute(int limit, Query.Mode mode)
      Deprecated.
      Execute the query using the specified limit in the specified Query.Mode.
      Parameters:
      limit - The maximum number of results, see class header (really).
      mode - The mode.
      Returns:
      The result of the query.
      Throws:
      PersistException - If an error occurred.
    • execute

      EntityList execute(Pagination pagination)
      Execute the query using the specified pagination parameters in Query.Mode.LAZY mode.
      Parameters:
      pagination - The offset and limit of the query.
      Returns:
      The result of the query.
    • execute

      EntityList execute(Pagination pagination, Query.Mode mode)
      Execute the query using the specified pagination parameters in the specified Query.Mode.
      Parameters:
      pagination - The offset and limit of the query.
      mode - The mode.
      Returns:
      The result of the query.
    • getOnlyResult

      Entity getOnlyResult()
      Executes and returns the single selected Entity by this query, or throws.
      Returns:
      The single selected Entity.
      Throws:
      PermanentPersistException - If the query does not select exactly one Entity. Note that it does not matter if the model is to-1 or to-many.
      See Also:
    • getOnlyResult

      Entity getOnlyResult(LockMode lockMode)
    • uniqueResult

      @Deprecated Entity uniqueResult() throws PersistException
      Deprecated.
      Executes and returns the single selected Entity by this query, or throws.
      Returns:
      The single selected Entity.
      Throws:
      PermanentPersistException - If the query does not select exactly one Entity. Note that it does not matter if the model is to-1 or to-many.
      PersistException
      See Also:
    • getOnlyResultOrNull

      @Nullable @Nullable Entity getOnlyResultOrNull()
      Executes and returns the single selected Entity by this query, or null if none, or throws.
      Returns:
      The single selected Entity, or null if none.
      Throws:
      PermanentPersistException - If the query selects more than one Entity. Note that it does not matter if the model is to-1 or to-many.
      See Also:
    • getOnlyResultOrNull

      @Nullable @Nullable Entity getOnlyResultOrNull(LockMode lockMode)
    • uniqueResultOrNull

      @Nullable @Deprecated @Nullable Entity uniqueResultOrNull() throws PersistException
      Deprecated.
      Executes and returns the single selected Entity by this query, or null if none, or throws.
      Returns:
      The single selected Entity, or null if none.
      Throws:
      PermanentPersistException - If the query selects more than one Entity. Note that it does not matter if the model is to-1 or to-many.
      PersistException
      See Also:
    • firstResult

      Entity firstResult() throws PersistException
      Executes and returns the first selected Entity by this query, or throws.
      Returns:
      The first selected Entity.
      Throws:
      PermanentPersistException - If the query does not select at least one Entity.
      PersistException
      See Also:
    • firstResultOrNull

      @Nullable @Nullable Entity firstResultOrNull() throws PersistException
      Executes and returns the first selected Entity by this query, or null if none.
      Returns:
      The first selected Entity, or null if none.
      Throws:
      PersistException - If an error occurred.
      See Also:
    • count

      int count() throws PersistException
      Currently badly implemented! Count the results.

      WARNING: This method does not behave as "SELECT count()" in SQL does! In fact it is temporarily implemented as a chaining of execute().size() only. No query modification is done etc. ALSO, the default query limit is used and thus if there would be more records, only the default limit is returned. The impl may be rewritten to be what one would expect; until then users may only use this method if they can live with today's limits: small counts.

      Speed of "SELECT count()" greatly depends on the underlying db table impl. An example are the two MySQL table types MyISAM and InnoDB. A query-less count on the former returns instantly because it keeps track of the number, whereas the latter needs to iterate over all records (at least in case of non-fixed length record tables) and can take minutes depending on the size.

      Returns:
      The count.
      Throws:
      PersistException - If an error occurs.
    • getManager

      EntityManager getManager()
      Get the entity manager.
      Returns:
      The entity manager.
    • toQueryDefinition

      QueryDefinition toQueryDefinition()