Class QueryImpl

  • Direct Known Subclasses:
    EJBQueryImpl, StoredProcedureQueryImpl

    public class QueryImpl
    extends java.lang.Object
    Concrete JPA query class. The JPA query wraps a DatabaseQuery which is executed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.eclipse.persistence.queries.DatabaseQuery databaseQuery
      Wrapped native query.
      protected EntityManagerImpl entityManager  
      protected int firstResultIndex  
      protected boolean isShared
      Stores if the wrapped query is shared, and requires cloning before being changed.
      protected javax.persistence.LockModeType lockMode  
      protected int maxResults  
      protected java.util.Map<java.lang.String,​javax.persistence.Parameter<?>> parameters  
      protected java.util.Map<java.lang.String,​java.lang.Object> parameterValues  
      protected java.lang.String queryName  
      static int UNDEFINED  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected QueryImpl​(EntityManagerImpl entityManager)
      Base constructor for EJBQueryImpl.
        QueryImpl​(org.eclipse.persistence.queries.DatabaseQuery query, EntityManagerImpl entityManager)
      Create an EJBQueryImpl with a DatabaseQuery.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected static void applyArguments​(org.eclipse.persistence.queries.StoredProcedureCall call, org.eclipse.persistence.queries.DatabaseQuery query)
      Define the query arguments based on the procedure call.
      protected static org.eclipse.persistence.queries.DatabaseQuery applyHints​(java.util.Map<java.lang.String,​java.lang.Object> hints, org.eclipse.persistence.queries.DatabaseQuery query, java.lang.ClassLoader classLoader, org.eclipse.persistence.internal.sessions.AbstractSession session)
      Set implementation-specific hints.
      protected void cloneSharedQuery()
      If the query was from the jpql parse cache it must be cloned before being modified.
      void close()
      This method should be called to close any left over open connection to the database (if there is one).
      protected java.lang.Object convertTemporalType​(java.lang.Object value, javax.persistence.TemporalType type)
      Convert the given object to the class represented by the given temporal type.
      protected java.lang.Object executeReadQuery()
      Execute a ReadQuery by assigning the stored parameter values and running it in the database
      int executeUpdate()
      Execute an update or delete statement.
      protected org.eclipse.persistence.sessions.Session getActiveSession()  
      org.eclipse.persistence.queries.DatabaseQuery getDatabaseQuery()
      Return the wrapped DatabaseQuery ensuring that if it isShared it is cloned before returning to prevent corruption of the query cache.
      org.eclipse.persistence.queries.DatabaseQuery getDatabaseQueryInternal()
      INTERNAL: Return the cached database query for this EJBQueryImpl.
      protected java.lang.RuntimeException getDetailedException​(org.eclipse.persistence.exceptions.DatabaseException e)
      Given a DatabaseException, this method will determine if we should throw a different more specific exception like a lock timeout exception.
      JpaEntityManager getEntityManager()
      Return the entityManager this query is tied to.
      int getFirstResult()  
      javax.persistence.FlushModeType getFlushMode()  
      java.util.Map<java.lang.String,​java.lang.Object> getHints()  
      protected java.util.Map<java.lang.String,​javax.persistence.Parameter<?>> getInternalParameters()
      Return the internal map of parameters.
      javax.persistence.LockModeType getLockMode()
      Get the current lock mode for the query.
      int getMaxResults()  
      int getMaxResultsInternal()  
      javax.persistence.Parameter<?> getParameter​(int position)  
      <T> javax.persistence.Parameter<T> getParameter​(int position, java.lang.Class<T> type)  
      javax.persistence.Parameter<?> getParameter​(java.lang.String name)  
      <T> javax.persistence.Parameter<T> getParameter​(java.lang.String name, java.lang.Class<T> type)  
      static java.lang.String getParameterId​(javax.persistence.Parameter param)
      Return the identifier of this parameter.
      java.util.Set<javax.persistence.Parameter<?>> getParameters()  
      java.lang.Object getParameterValue​(int position)
      Return the value bound to the positional parameter.
      java.lang.Object getParameterValue​(java.lang.String name)
      Return the value bound to the named parameter.
      <T> T getParameterValue​(javax.persistence.Parameter<T> param)  
      java.util.List getResultList()
      Execute the query and return the query results as a List.
      java.lang.Object getSingleResult()
      Execute a SELECT query that returns a single untyped result.
      java.util.Set<java.lang.String> getSupportedHints()  
      boolean isBound​(javax.persistence.Parameter<?> param)
      Return a boolean indicating whether a value has been bound to the parameter.
      protected boolean isFlushModeAUTO()
      Spec.
      protected boolean isValidActualParameter​(java.lang.Object value, java.lang.Class parameterType)  
      protected void performPreQueryFlush()  
      protected java.util.List<java.lang.Object> processParameters()
      Internal method to add the parameters values to the query prior to execution.
      protected void propagateResultProperties()
      Configure the firstResult, maxRows and lock mode in the EclipseLink ReadQuery.
      protected void setAsDataModifyQuery()
      INTERNAL: Change the internal query to data modify query.
      protected void setAsSQLModifyQuery()
      Internal method to change the wrapped query to a DataModifyQuery if necessary.
      protected void setAsSQLReadQuery()
      Internal method to change the wrapped query to a DataReadQuery if necessary.
      void setDatabaseQuery​(org.eclipse.persistence.queries.DatabaseQuery query)
      Replace the cached query with the given query.
      QueryImpl setFirstResult​(int startPosition)
      Set the position of the first result to retrieve.
      protected void setFirstResultInternal​(int startPosition)
      Set the position of the first result to retrieve.
      QueryImpl setFlushMode​(javax.persistence.FlushModeType flushMode)
      Set the flush mode type to be used for the query execution.
      protected void setHintInternal​(java.lang.String hintName, java.lang.Object value)
      Set an implementation-specific hint.
      QueryImpl setLockMode​(javax.persistence.LockModeType lockMode)
      Set the lock mode type to be used for the query execution.
      QueryImpl setMaxResults​(int maxResult)
      Set the maximum number of results to retrieve.
      void setMaxResultsInternal​(int maxResult)
      Set the maximum number of results to retrieve.
      protected void setParameterInternal​(int position, java.lang.Object value)
      Bind an argument to a positional parameter.
      protected void setParameterInternal​(java.lang.String name, java.lang.Object value, boolean isIndex)
      Bind an argument to a named or indexed parameter.
      protected void setRollbackOnly()  
      protected void throwNonUniqueResultException​(java.lang.String message)  
      protected void throwNoResultException​(java.lang.String message)  
      java.lang.String toString()  
      <T> T unwrap​(java.lang.Class<T> cls)
      Unwrap the query into the JPA implementation classes/interfaces or the underlying native EclipseLink query.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • databaseQuery

        protected org.eclipse.persistence.queries.DatabaseQuery databaseQuery
        Wrapped native query. The query may be isShared
      • queryName

        protected java.lang.String queryName
      • parameterValues

        protected java.util.Map<java.lang.String,​java.lang.Object> parameterValues
      • parameters

        protected java.util.Map<java.lang.String,​javax.persistence.Parameter<?>> parameters
      • firstResultIndex

        protected int firstResultIndex
      • maxResults

        protected int maxResults
      • lockMode

        protected javax.persistence.LockModeType lockMode
      • isShared

        protected boolean isShared
        Stores if the wrapped query is shared, and requires cloning before being changed.
    • Constructor Detail

      • QueryImpl

        protected QueryImpl​(EntityManagerImpl entityManager)
        Base constructor for EJBQueryImpl. Initializes basic variables.
      • QueryImpl

        public QueryImpl​(org.eclipse.persistence.queries.DatabaseQuery query,
                         EntityManagerImpl entityManager)
        Create an EJBQueryImpl with a DatabaseQuery.
    • Method Detail

      • close

        public void close()
        This method should be called to close any left over open connection to the database (if there is one).
      • setAsDataModifyQuery

        protected void setAsDataModifyQuery()
        INTERNAL: Change the internal query to data modify query.
      • setAsSQLModifyQuery

        protected void setAsSQLModifyQuery()
        Internal method to change the wrapped query to a DataModifyQuery if necessary. When created, the query is created as a DataReadQuery as it is unknown if it is a SELECT or UPDATE. Note that this prevents the original named query from every being prepared.
      • setAsSQLReadQuery

        protected void setAsSQLReadQuery()
        Internal method to change the wrapped query to a DataReadQuery if necessary. This should never occur, but could possibly if the same query was executed as executeUpdate() then as getResultList(). Note that the initial conversion to modify would loose any read settings that had been set.
      • executeReadQuery

        protected java.lang.Object executeReadQuery()
        Execute a ReadQuery by assigning the stored parameter values and running it in the database
        Returns:
        the results of the query execution
      • executeUpdate

        public int executeUpdate()
        Execute an update or delete statement.
        Returns:
        the number of entities updated or deleted
      • getDatabaseQuery

        public org.eclipse.persistence.queries.DatabaseQuery getDatabaseQuery()
        Return the wrapped DatabaseQuery ensuring that if it isShared it is cloned before returning to prevent corruption of the query cache.
        See Also:
        getDatabaseQueryInternal()
      • getDatabaseQueryInternal

        public org.eclipse.persistence.queries.DatabaseQuery getDatabaseQueryInternal()
        INTERNAL: Return the cached database query for this EJBQueryImpl. If the query is a named query and it has not yet been looked up, the query will be looked up and stored as the cached query.
      • getDetailedException

        protected java.lang.RuntimeException getDetailedException​(org.eclipse.persistence.exceptions.DatabaseException e)
        Given a DatabaseException, this method will determine if we should throw a different more specific exception like a lock timeout exception.
      • getEntityManager

        public JpaEntityManager getEntityManager()
        Return the entityManager this query is tied to.
      • getInternalParameters

        protected java.util.Map<java.lang.String,​javax.persistence.Parameter<?>> getInternalParameters()
        Return the internal map of parameters.
      • getLockMode

        public javax.persistence.LockModeType getLockMode()
        Get the current lock mode for the query.
        Returns:
        lock mode
        Throws:
        java.lang.IllegalStateException - if not a Java Persistence query language SELECT query
      • getResultList

        public java.util.List getResultList()
        Execute the query and return the query results as a List.
        Returns:
        a list of the results
      • getSingleResult

        public java.lang.Object getSingleResult()
        Execute a SELECT query that returns a single untyped result.
        Returns:
        the result
        Throws:
        javax.persistence.NoResultException - if there is no result
        javax.persistence.NonUniqueResultException - if more than one result
        java.lang.IllegalStateException - if called for a Java Persistence query language UPDATE or DELETE statement
        javax.persistence.QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
        javax.persistence.TransactionRequiredException - if a lock mode other than NONE has been been set and there is no transaction or the persistence context has not been joined to the transaction
        javax.persistence.PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
        javax.persistence.LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
        javax.persistence.PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
      • processParameters

        protected java.util.List<java.lang.Object> processParameters()
        Internal method to add the parameters values to the query prior to execution. Returns a list of parameter values in the order the parameters are defined for the databaseQuery.
      • setDatabaseQuery

        public void setDatabaseQuery​(org.eclipse.persistence.queries.DatabaseQuery query)
        Replace the cached query with the given query.
      • setFirstResult

        public QueryImpl setFirstResult​(int startPosition)
        Set the position of the first result to retrieve.
        Parameters:
        startPosition - position of the first result, numbered from 0
        Returns:
        the same query instance
      • getFirstResult

        public int getFirstResult()
        Since:
        Java Persistence API 2.0
        See Also:
        Query.getFirstResult()
      • setFlushMode

        public QueryImpl setFlushMode​(javax.persistence.FlushModeType flushMode)
        Set the flush mode type to be used for the query execution.
        Parameters:
        flushMode -
      • setFirstResultInternal

        protected void setFirstResultInternal​(int startPosition)
        Set the position of the first result to retrieve.
        Parameters:
        startPosition - position of the first result, numbered from 0.
      • applyArguments

        protected static void applyArguments​(org.eclipse.persistence.queries.StoredProcedureCall call,
                                             org.eclipse.persistence.queries.DatabaseQuery query)
        Define the query arguments based on the procedure call.
      • applyHints

        protected static org.eclipse.persistence.queries.DatabaseQuery applyHints​(java.util.Map<java.lang.String,​java.lang.Object> hints,
                                                                                  org.eclipse.persistence.queries.DatabaseQuery query,
                                                                                  java.lang.ClassLoader classLoader,
                                                                                  org.eclipse.persistence.internal.sessions.AbstractSession session)
        Set implementation-specific hints.
        Parameters:
        hints - a list of hints to be applied to the query
        query - the query to apply the hints to
      • getParameterId

        public static java.lang.String getParameterId​(javax.persistence.Parameter param)
        Return the identifier of this parameter. This will be the name if it is set, else it will be the position
        Parameters:
        param -
        Returns:
      • isBound

        public boolean isBound​(javax.persistence.Parameter<?> param)
        Return a boolean indicating whether a value has been bound to the parameter.
        Parameters:
        param - parameter object
        Returns:
        boolean indicating whether parameter has been bound
      • isFlushModeAUTO

        protected boolean isFlushModeAUTO()
        Spec. 3.5.2: "FlushMode.AUTO is set on the Query object, or if the flush mode setting for the persistence context is AUTO (the default) and a flush mode setting has not been specified for the Query object, the persistence provider is responsible for ensuring that all updates to the state of all entities in the persistence context which could potentially affect the result of the query are visible to the processing of the query."
      • setHintInternal

        protected void setHintInternal​(java.lang.String hintName,
                                       java.lang.Object value)
        Set an implementation-specific hint. If the hint name is not recognized, it is silently ignored.
        Throws:
        java.lang.IllegalArgumentException - if the second argument is not valid for the implementation.
      • setLockMode

        public QueryImpl setLockMode​(javax.persistence.LockModeType lockMode)
        Set the lock mode type to be used for the query execution.
        Parameters:
        lockMode -
        Throws:
        java.lang.IllegalStateException - if not a Java Persistence query language SELECT query
      • cloneSharedQuery

        protected void cloneSharedQuery()
        If the query was from the jpql parse cache it must be cloned before being modified.
      • convertTemporalType

        protected java.lang.Object convertTemporalType​(java.lang.Object value,
                                                       javax.persistence.TemporalType type)
        Convert the given object to the class represented by the given temporal type.
        Returns:
        an object representing the given TemporalType.
      • setMaxResults

        public QueryImpl setMaxResults​(int maxResult)
        Set the maximum number of results to retrieve.
        Parameters:
        maxResult -
        Returns:
        the same query instance
      • getMaxResults

        public int getMaxResults()
        Since:
        Java Persistence API 2.0
        See Also:
        Query.getMaxResults()
      • getMaxResultsInternal

        public int getMaxResultsInternal()
        Since:
        Java Persistence API 2.0
        See Also:
        Query.getMaxResults()
      • setMaxResultsInternal

        public void setMaxResultsInternal​(int maxResult)
        Set the maximum number of results to retrieve.
        Parameters:
        maxResult -
      • propagateResultProperties

        protected void propagateResultProperties()
        Configure the firstResult, maxRows and lock mode in the EclipseLink ReadQuery.
      • setParameterInternal

        protected void setParameterInternal​(int position,
                                            java.lang.Object value)
        Bind an argument to a positional parameter.
        Parameters:
        position -
        value -
      • setParameterInternal

        protected void setParameterInternal​(java.lang.String name,
                                            java.lang.Object value,
                                            boolean isIndex)
        Bind an argument to a named or indexed parameter.
        Parameters:
        name - the parameter name
        value - to bind
        isIndex - defines if index or named
      • isValidActualParameter

        protected boolean isValidActualParameter​(java.lang.Object value,
                                                 java.lang.Class parameterType)
      • getActiveSession

        protected org.eclipse.persistence.sessions.Session getActiveSession()
      • performPreQueryFlush

        protected void performPreQueryFlush()
      • setRollbackOnly

        protected void setRollbackOnly()
      • throwNoResultException

        protected void throwNoResultException​(java.lang.String message)
      • throwNonUniqueResultException

        protected void throwNonUniqueResultException​(java.lang.String message)
      • getFlushMode

        public javax.persistence.FlushModeType getFlushMode()
        Since:
        Java Persistence 2.0
        See Also:
        Query.getFlushMode()
      • getHints

        public java.util.Map<java.lang.String,​java.lang.Object> getHints()
        Since:
        Java Persistence 2.0
        See Also:
        Query.getHints()
      • getParameter

        public <T> javax.persistence.Parameter<T> getParameter​(java.lang.String name,
                                                               java.lang.Class<T> type)
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameter(String, Class)
      • getParameter

        public <T> javax.persistence.Parameter<T> getParameter​(int position,
                                                               java.lang.Class<T> type)
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameter(int, Class)
      • getParameter

        public javax.persistence.Parameter<?> getParameter​(java.lang.String name)
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameter(String, Class)
      • getParameter

        public javax.persistence.Parameter<?> getParameter​(int position)
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameter(int, Class)
      • getParameterValue

        public <T> T getParameterValue​(javax.persistence.Parameter<T> param)
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameterValue(Parameter)
      • getParameterValue

        public java.lang.Object getParameterValue​(java.lang.String name)
        Return the value bound to the named parameter.
        Parameters:
        name -
        Returns:
        parameter value
        Throws:
        java.lang.IllegalStateException - if the parameter has not been been bound
      • getParameterValue

        public java.lang.Object getParameterValue​(int position)
        Return the value bound to the positional parameter.
        Parameters:
        position -
        Returns:
        parameter value
        Throws:
        java.lang.IllegalStateException - if the parameter has not been been bound
      • getParameters

        public java.util.Set<javax.persistence.Parameter<?>> getParameters()
        Since:
        Java Persistence 2.0
        See Also:
        Query.getParameters()
      • getSupportedHints

        public java.util.Set<java.lang.String> getSupportedHints()
        Since:
        Java Persistence 2.0
      • unwrap

        public <T> T unwrap​(java.lang.Class<T> cls)
        Unwrap the query into the JPA implementation classes/interfaces or the underlying native EclipseLink query.
        Since:
        Java Persistence 2.0
        See Also:
        Query.unwrap(Class)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object