Class DetachedCriteria

  • All Implemented Interfaces:
    Serializable, CriteriaSpecification

    public class DetachedCriteria
    extends Object
    implements CriteriaSpecification, Serializable
    Models a detached form of a Criteria (not associated with a Session). Some applications need to create criteria queries in "detached mode", where the Hibernate Session is not available. Applications would create a DetachableCriteria to describe the query, and then later associated it with a Session to obtain the "executable" Criteria: DetachedCriteria detached = new DetachedCriteria(); ... Criteria criteria = detached.getExecutableCriteria( session ); ... criteria.list(); All methods have the same semantics and behavior as the corresponding methods of the Criteria interface.
    See Also:
    Criteria, Serialized Form
    • Constructor Detail

      • DetachedCriteria

        protected DetachedCriteria​(String entityName)
      • DetachedCriteria

        protected DetachedCriteria​(String entityName,
                                   String alias)
      • DetachedCriteria

        protected DetachedCriteria​(org.hibernate.internal.CriteriaImpl impl,
                                   Criteria criteria)
    • Method Detail

      • getExecutableCriteria

        public Criteria getExecutableCriteria​(Session session)
        Get an executable instance of Criteria to actually run the query.
        Parameters:
        session - The session to associate the built Criteria with
        Returns:
        The "executable" Criteria
      • getAlias

        public String getAlias()
        Obtain the alias associated with this DetachedCriteria
        Returns:
        The alias
      • forEntityName

        public static DetachedCriteria forEntityName​(String entityName)
        Static builder to create a DetachedCriteria for the given entity.
        Parameters:
        entityName - The name of the entity to create a DetachedCriteria for
        Returns:
        The DetachedCriteria
      • forEntityName

        public static DetachedCriteria forEntityName​(String entityName,
                                                     String alias)
        Static builder to create a DetachedCriteria for the given entity.
        Parameters:
        entityName - The name of the entity to create a DetachedCriteria for
        alias - The alias to apply to the entity
        Returns:
        The DetachedCriteria
      • forClass

        public static DetachedCriteria forClass​(Class clazz)
        Static builder to create a DetachedCriteria for the given entity, by its Class.
        Parameters:
        clazz - The entity class
        Returns:
        The DetachedCriteria
      • forClass

        public static DetachedCriteria forClass​(Class clazz,
                                                String alias)
        Static builder to create a DetachedCriteria for the given entity, by its Class.
        Parameters:
        clazz - The entity class
        alias - The alias to apply to the entity
        Returns:
        The DetachedCriteria
      • add

        public DetachedCriteria add​(Criterion criterion)
        Add a restriction
        Parameters:
        criterion - The restriction
        Returns:
        this, for method chaining
      • addOrder

        public DetachedCriteria addOrder​(Order order)
        Adds an ordering
        Parameters:
        order - The ordering
        Returns:
        this, for method chaining
      • setFetchMode

        public DetachedCriteria setFetchMode​(String associationPath,
                                             FetchMode mode)
        Set the fetch mode for a given association
        Parameters:
        associationPath - The association path
        mode - The fetch mode to apply
        Returns:
        this, for method chaining
      • setProjection

        public DetachedCriteria setProjection​(Projection projection)
        Set the projection to use.
        Parameters:
        projection - The projection to use
        Returns:
        this, for method chaining
      • setResultTransformer

        public DetachedCriteria setResultTransformer​(ResultTransformer resultTransformer)
        Set the result transformer to use.
        Parameters:
        resultTransformer - The result transformer to use
        Returns:
        this, for method chaining
      • createAlias

        public DetachedCriteria createAlias​(String associationPath,
                                            String alias)
        Creates an association path alias within this DetachedCriteria. The alias can then be used in further alias creations or restrictions, etc.
        Parameters:
        associationPath - The association path
        alias - The alias to apply to that association path
        Returns:
        this, for method chaining
      • createAlias

        public DetachedCriteria createAlias​(String associationPath,
                                            String alias,
                                            JoinType joinType)
        Creates an association path alias within this DetachedCriteria specifying the type of join. The alias can then be used in further alias creations or restrictions, etc.
        Parameters:
        associationPath - The association path
        alias - The alias to apply to that association path
        joinType - The type of join to use
        Returns:
        this, for method chaining
      • createAlias

        public DetachedCriteria createAlias​(String associationPath,
                                            String alias,
                                            JoinType joinType,
                                            Criterion withClause)
        Creates an association path alias within this DetachedCriteria specifying the type of join. The alias can then be used in further alias creations or restrictions, etc.
        Parameters:
        associationPath - The association path
        alias - The alias to apply to that association path
        joinType - The type of join to use
        withClause - An additional restriction on the join
        Returns:
        this, for method chaining
      • createCriteria

        public DetachedCriteria createCriteria​(String associationPath,
                                               String alias)
        Creates a nested DetachedCriteria representing the association path.
        Parameters:
        associationPath - The association path
        alias - The alias to apply to that association path
        Returns:
        the newly created, nested DetachedCriteria
      • createCriteria

        public DetachedCriteria createCriteria​(String associationPath)
        Creates a nested DetachedCriteria representing the association path.
        Parameters:
        associationPath - The association path
        Returns:
        the newly created, nested DetachedCriteria
      • createCriteria

        public DetachedCriteria createCriteria​(String associationPath,
                                               JoinType joinType)
        Creates a nested DetachedCriteria representing the association path, specifying the type of join to use.
        Parameters:
        associationPath - The association path
        joinType - The type of join to use
        Returns:
        the newly created, nested DetachedCriteria
      • createCriteria

        public DetachedCriteria createCriteria​(String associationPath,
                                               String alias,
                                               JoinType joinType)
        Creates a nested DetachedCriteria representing the association path, specifying the type of join to use.
        Parameters:
        associationPath - The association path
        alias - The alias to associate with this "join".
        joinType - The type of join to use
        Returns:
        the newly created, nested DetachedCriteria
      • createCriteria

        public DetachedCriteria createCriteria​(String associationPath,
                                               String alias,
                                               JoinType joinType,
                                               Criterion withClause)
        Creates a nested DetachedCriteria representing the association path, specifying the type of join to use and an additional join restriction.
        Parameters:
        associationPath - The association path
        alias - The alias to associate with this "join".
        joinType - The type of join to use
        withClause - The additional join restriction
        Returns:
        the newly created, nested DetachedCriteria
      • setComment

        public DetachedCriteria setComment​(String comment)
        Set the SQL comment to use.
        Parameters:
        comment - The SQL comment to use
        Returns:
        this, for method chaining
      • setLockMode

        public DetachedCriteria setLockMode​(LockMode lockMode)
        Set the lock mode to use.
        Parameters:
        lockMode - The lock mode to use
        Returns:
        this, for method chaining
      • setLockMode

        public DetachedCriteria setLockMode​(String alias,
                                            LockMode lockMode)
        Set an alias-specific lock mode. The specified lock mode applies only to that alias.
        Parameters:
        alias - The alias to apply the lock to
        lockMode - The lock mode to use.
        Returns:
        this, for method chaining
      • setTimeout

        public DetachedCriteria setTimeout​(int timeout)
        Set a timeout for the underlying JDBC query.
        Parameters:
        timeout - The timeout value to apply.
        Returns:
        this (for method chaining)
        See Also:
        Statement.setQueryTimeout(int)