Package org.datanucleus.api.jpa
Class JPAQuery<X>
- java.lang.Object
-
- org.datanucleus.api.jpa.JPAQuery<X>
-
- Type Parameters:
X- Type of the candidate of the query
- All Implemented Interfaces:
javax.persistence.Query,javax.persistence.TypedQuery<X>
- Direct Known Subclasses:
JPAStoredProcedureQuery
public class JPAQuery<X> extends Object implements javax.persistence.TypedQuery<X>
Basic implementation of a JPA Query. Wraps an internal query.
-
-
Field Summary
Fields Modifier and Type Field Description static StringQUERY_HINT_FETCH_SIZEstatic StringQUERY_HINT_IGNORE_CACHEstatic StringQUERY_HINT_TIMEOUT
-
Constructor Summary
Constructors Constructor Description JPAQuery(JPAEntityManager em, org.datanucleus.store.query.Query query, String language)Constructor for a query used by JPA.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intexecuteUpdate()Method to execute a (UPDATE/DELETE) query returning the number of changed records.JPAFetchPlangetFetchPlan()intgetFirstResult()The position of the first result the query object was set to retrieve.javax.persistence.FlushModeTypegetFlushMode()The flush mode in effect for the query execution.Map<String,Object>getHints()Get the hints and associated values that are in effect for the query instance.org.datanucleus.store.query.QuerygetInternalQuery()Accessor for the internal query.StringgetLanguage()Accessor for the query language.javax.persistence.LockModeTypegetLockMode()intgetMaxResults()The maximum number of results the query object was set to retrieve.ObjectgetNativeQuery()Accessor for the native query invoked by this query (if known at this time and supported by the store plugin).javax.persistence.Parameter<?>getParameter(int position)<T> javax.persistence.Parameter<T>getParameter(int position, Class<T> type)Get the positional parameter with the given position and type.javax.persistence.Parameter<?>getParameter(String name)<T> javax.persistence.Parameter<T>getParameter(String name, Class<T> type)Get the parameter of the given name and type.Set<javax.persistence.Parameter<?>>getParameters()Get the query parameter objects.ObjectgetParameterValue(int position)ObjectgetParameterValue(String name)<T> TgetParameterValue(javax.persistence.Parameter<T> param)Return the value that has been bound to the parameter.ListgetResultList()Method to execute a (SELECT) query statement returning multiple results.XgetSingleResult()Method to execute a SELECT statement returning a single result.Set<String>getSupportedHints()Get the names of the hints that are supported for query objects.booleanisBound(javax.persistence.Parameter<?> param)protected booleanisNativeQuery()protected voidloadParameters()protected voidloadParametersForCompilation(org.datanucleus.store.query.compiler.QueryCompilation compilation)voidsaveAsNamedQuery(String name)Save this query as a named query with the specified name.javax.persistence.TypedQuery<X>setFirstResult(int startPosition)Method to set the results to start from a particular position.javax.persistence.TypedQuery<X>setFlushMode(javax.persistence.FlushModeType mode)Mutator for the flush mode.javax.persistence.TypedQuery<X>setHint(String hintName, Object value)Method to add a vendor extension to the query.javax.persistence.TypedQuery<X>setLockMode(javax.persistence.LockModeType lock)javax.persistence.TypedQuery<X>setMaxResults(int max)Method to set the max number of results to return.javax.persistence.TypedQuery<X>setParameter(int position, Object value)Bind an argument to a positional parameter.javax.persistence.TypedQuery<X>setParameter(int position, Calendar value, javax.persistence.TemporalType temporalType)Bind an instance of java.util.Calendar to a positional parameter.javax.persistence.TypedQuery<X>setParameter(int position, Date value, javax.persistence.TemporalType temporalType)Bind an instance of java.util.Date to a positional parameter.javax.persistence.TypedQuery<X>setParameter(String name, Object value)Bind an argument to a named parameter.javax.persistence.TypedQuery<X>setParameter(String name, Calendar value, javax.persistence.TemporalType temporalType)Bind an instance of java.util.Calendar to a named parameter.javax.persistence.TypedQuery<X>setParameter(String name, Date value, javax.persistence.TemporalType temporalType)Bind an instance of java.util.Date to a named parameter.JPAQuery<X>setParameter(javax.persistence.Parameter<Calendar> param, Calendar cal, javax.persistence.TemporalType type)javax.persistence.TypedQuery<X>setParameter(javax.persistence.Parameter<Date> param, Date date, javax.persistence.TemporalType type)<T> javax.persistence.TypedQuery<X>setParameter(javax.persistence.Parameter<T> param, T value)Bind the value of a Parameter object.StringtoString()Method to return the single-string form of the query.<T> Tunwrap(Class<T> cls)Return an object of the specified type to allow access to the provider-specific API.
-
-
-
Field Detail
-
QUERY_HINT_TIMEOUT
public static final String QUERY_HINT_TIMEOUT
- See Also:
- Constant Field Values
-
QUERY_HINT_FETCH_SIZE
public static final String QUERY_HINT_FETCH_SIZE
-
QUERY_HINT_IGNORE_CACHE
public static final String QUERY_HINT_IGNORE_CACHE
-
-
Constructor Detail
-
JPAQuery
public JPAQuery(JPAEntityManager em, org.datanucleus.store.query.Query query, String language)
Constructor for a query used by JPA.- Parameters:
em- Entity Managerquery- Underlying querylanguage- Query language
-
-
Method Detail
-
getFetchPlan
public JPAFetchPlan getFetchPlan()
-
executeUpdate
public int executeUpdate()
Method to execute a (UPDATE/DELETE) query returning the number of changed records.- Specified by:
executeUpdatein interfacejavax.persistence.Query- Returns:
- Number of records updated/deleted with the query.
- Throws:
javax.persistence.QueryTimeoutException- if the query times out
-
getResultList
public List getResultList()
Method to execute a (SELECT) query statement returning multiple results.- Specified by:
getResultListin interfacejavax.persistence.Query- Specified by:
getResultListin interfacejavax.persistence.TypedQuery<X>- Returns:
- The results
- Throws:
javax.persistence.QueryTimeoutException- if the query times out
-
getSingleResult
public X getSingleResult()
Method to execute a SELECT statement returning a single result.- Specified by:
getSingleResultin interfacejavax.persistence.Query- Specified by:
getSingleResultin interfacejavax.persistence.TypedQuery<X>- Returns:
- the result
- Throws:
javax.persistence.QueryTimeoutException- if the query times out
-
setFirstResult
public javax.persistence.TypedQuery<X> setFirstResult(int startPosition)
Method to set the results to start from a particular position.- Specified by:
setFirstResultin interfacejavax.persistence.Query- Specified by:
setFirstResultin interfacejavax.persistence.TypedQuery<X>- Parameters:
startPosition- position of first result numbered from 0- Returns:
- The query
-
setMaxResults
public javax.persistence.TypedQuery<X> setMaxResults(int max)
Method to set the max number of results to return.- Specified by:
setMaxResultsin interfacejavax.persistence.Query- Specified by:
setMaxResultsin interfacejavax.persistence.TypedQuery<X>- Parameters:
max- Number of results max- Returns:
- The query
-
getMaxResults
public int getMaxResults()
The maximum number of results the query object was set to retrieve. Returns Integer.MAX_VALUE if setMaxResults was not applied to the query object.- Specified by:
getMaxResultsin interfacejavax.persistence.Query- Returns:
- maximum number of results
-
getFirstResult
public int getFirstResult()
The position of the first result the query object was set to retrieve. Returns 0 if setFirstResult was not applied to the query object.- Specified by:
getFirstResultin interfacejavax.persistence.Query- Returns:
- position of first result
-
setFlushMode
public javax.persistence.TypedQuery<X> setFlushMode(javax.persistence.FlushModeType mode)
Mutator for the flush mode.- Specified by:
setFlushModein interfacejavax.persistence.Query- Specified by:
setFlushModein interfacejavax.persistence.TypedQuery<X>- Parameters:
mode- Flush mode- Returns:
- The query
-
getFlushMode
public javax.persistence.FlushModeType getFlushMode()
The flush mode in effect for the query execution. If a flush mode has not been set for the query object, returns the flush mode in effect for the entity manager.- Specified by:
getFlushModein interfacejavax.persistence.Query- Returns:
- flush mode
-
setHint
public javax.persistence.TypedQuery<X> setHint(String hintName, Object value)
Method to add a vendor extension to the query. If the hint name is not recognized, it is silently ignored.- Specified by:
setHintin interfacejavax.persistence.Query- Specified by:
setHintin interfacejavax.persistence.TypedQuery<X>- Parameters:
hintName- Name of the "hint"value- Value for the "hint"- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if the second argument is not valid for the implementation
-
getHints
public Map<String,Object> getHints()
Get the hints and associated values that are in effect for the query instance.- Specified by:
getHintsin interfacejavax.persistence.Query- Returns:
- query hints
-
getSupportedHints
public Set<String> getSupportedHints()
Get the names of the hints that are supported for query objects. These hints correspond to hints that may be passed to the methods of the Query interface that take hints as arguments or used with the NamedQuery and NamedNativeQuery annotations. These include all standard query hints as well as vendor-specific hints supported by the provider. These hints may or may not currently be in effect.- Returns:
- hints
-
setParameter
public <T> javax.persistence.TypedQuery<X> setParameter(javax.persistence.Parameter<T> param, T value)
Bind the value of a Parameter object.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
param- parameter to be setvalue- parameter value- Returns:
- query instance
- Throws:
IllegalArgumentException- if parameter does not correspond to a parameter of the query
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(String name, Object value)
Bind an argument to a named parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
name- the parameter namevalue- The value for the param- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if parameter name does not correspond to parameter in query string or argument is of incorrect type
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(int position, Object value)
Bind an argument to a positional parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
position- Parameter positionvalue- The value- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if position does not correspond to positional parameter of query or argument is of incorrect type
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(String name, Date value, javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Date to a named parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
name- Name of the paramvalue- Value for the paramtemporalType- The temporal type- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if parameter name does not correspond to parameter in query string
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(String name, Calendar value, javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Calendar to a named parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
name- name of the paramvalue- Value for the paramtemporalType- The temporal type- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if parameter name does not correspond to parameter in query string
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(int position, Date value, javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Date to a positional parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
position- Parameter positionvalue- Value for the paramtemporalType- Temporal Type- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if position does not correspond to positional parameter of query
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(int position, Calendar value, javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Calendar to a positional parameter.- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>- Parameters:
position- Parameter positionvalue- Value for the paramtemporalType- Temporal type- Returns:
- the same query instance
- Throws:
IllegalArgumentException- if position does not correspond to positional parameter of query
-
setParameter
public JPAQuery<X> setParameter(javax.persistence.Parameter<Calendar> param, Calendar cal, javax.persistence.TemporalType type)
- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>
-
setParameter
public javax.persistence.TypedQuery<X> setParameter(javax.persistence.Parameter<Date> param, Date date, javax.persistence.TemporalType type)
- Specified by:
setParameterin interfacejavax.persistence.Query- Specified by:
setParameterin interfacejavax.persistence.TypedQuery<X>
-
getInternalQuery
public org.datanucleus.store.query.Query getInternalQuery()
Accessor for the internal query.- Returns:
- Internal query
-
unwrap
public <T> T unwrap(Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API. If the provider's Query implementation does not support the specified class, the PersistenceException is thrown.- Specified by:
unwrapin interfacejavax.persistence.Query- Parameters:
cls- the class of the object to be returned. This is normally either the underlying Query implementation class or an interface that it implements.- Returns:
- an instance of the specified class
- Throws:
javax.persistence.PersistenceException- if the provider does not support the call.
-
getLanguage
public String getLanguage()
Accessor for the query language.- Returns:
- Query language
-
getParameters
public Set<javax.persistence.Parameter<?>> getParameters()
Get the query parameter objects. Returns empty set if the query has no parameters.- Specified by:
getParametersin interfacejavax.persistence.Query- Returns:
- parameter objects
-
loadParameters
protected void loadParameters()
-
loadParametersForCompilation
protected void loadParametersForCompilation(org.datanucleus.store.query.compiler.QueryCompilation compilation)
-
getParameter
public <T> javax.persistence.Parameter<T> getParameter(String name, Class<T> type)
Get the parameter of the given name and type.- Specified by:
getParameterin interfacejavax.persistence.Query- Returns:
- parameter object
- Throws:
IllegalArgumentException- if the parameter of the specified name and type doesn't exist
-
getParameter
public <T> javax.persistence.Parameter<T> getParameter(int position, Class<T> type)Get the positional parameter with the given position and type.- Specified by:
getParameterin interfacejavax.persistence.Query- Returns:
- parameter object
- Throws:
IllegalArgumentException- if the parameter with the specified position and type doesn't exist
-
getParameter
public javax.persistence.Parameter<?> getParameter(int position)
- Specified by:
getParameterin interfacejavax.persistence.Query
-
getParameter
public javax.persistence.Parameter<?> getParameter(String name)
- Specified by:
getParameterin interfacejavax.persistence.Query
-
getParameterValue
public <T> T getParameterValue(javax.persistence.Parameter<T> param)
Return the value that has been bound to the parameter.- Specified by:
getParameterValuein interfacejavax.persistence.Query- Parameters:
param- parameter object- Returns:
- parameter value
- Throws:
IllegalStateException- if the parameter has not been bound
-
getParameterValue
public Object getParameterValue(int position)
- Specified by:
getParameterValuein interfacejavax.persistence.Query
-
getParameterValue
public Object getParameterValue(String name)
- Specified by:
getParameterValuein interfacejavax.persistence.Query
-
isBound
public boolean isBound(javax.persistence.Parameter<?> param)
- Specified by:
isBoundin interfacejavax.persistence.Query
-
getLockMode
public javax.persistence.LockModeType getLockMode()
- Specified by:
getLockModein interfacejavax.persistence.Query
-
setLockMode
public javax.persistence.TypedQuery<X> setLockMode(javax.persistence.LockModeType lock)
- Specified by:
setLockModein interfacejavax.persistence.Query- Specified by:
setLockModein interfacejavax.persistence.TypedQuery<X>
-
toString
public String toString()
Method to return the single-string form of the query. Note that the JPA spec doesn't define this methods handling and this is an extension.
-
getNativeQuery
public Object getNativeQuery()
Accessor for the native query invoked by this query (if known at this time and supported by the store plugin).- Returns:
- The native query (e.g for RDBMS this is the SQL).
-
saveAsNamedQuery
public void saveAsNamedQuery(String name)
Save this query as a named query with the specified name. See also EntityManagerFactory.addNamedQuery(String, Query)- Parameters:
name- The name to refer to it under
-
isNativeQuery
protected boolean isNativeQuery()
-
-