org.datanucleus.jpa
Class JPAQuery<X>

java.lang.Object
  extended by org.datanucleus.jpa.JPAQuery<X>
All Implemented Interfaces:
javax.persistence.Query, javax.persistence.TypedQuery<X>

public class JPAQuery<X>
extends java.lang.Object
implements javax.persistence.TypedQuery<X>

Basic implementation of a JPA Query. Wraps an internal query.


Field Summary
protected static org.datanucleus.util.Localiser LOCALISER
          Localisation utility for output messages
 
Constructor Summary
JPAQuery(javax.persistence.EntityManager em, org.datanucleus.store.query.Query query, java.lang.String language)
          Constructor for a query used by JPA.
 
Method Summary
 int executeUpdate()
          Method to execute a (UPDATE/DELETE) query returning the number of changed records.
 int getFirstResult()
          The position of the first result the query object was set to retrieve.
 javax.persistence.FlushModeType getFlushMode()
          The flush mode in effect for the query execution.
 java.util.Map getHints()
          Get the hints and associated values that are in effect for the query instance.
 org.datanucleus.store.query.Query getInternalQuery()
          Accessor for the internal query.
 java.lang.String getLanguage()
          Accessor for the query language.
 javax.persistence.LockModeType getLockMode()
           
 int getMaxResults()
          The maximum number of results the query object was set to retrieve.
 javax.persistence.Parameter<?> getParameter(int position)
           
<T> javax.persistence.Parameter<T>
getParameter(int position, java.lang.Class<T> type)
          Get the positional parameter with the given position and type.
 javax.persistence.Parameter<?> getParameter(java.lang.String name)
           
<T> javax.persistence.Parameter<T>
getParameter(java.lang.String name, java.lang.Class<T> type)
          Get the parameter of the given name and type.
 java.util.Set<javax.persistence.Parameter<?>> getParameters()
          Get the query parameter objects.
 java.lang.Object getParameterValue(int position)
           
<T> T
getParameterValue(javax.persistence.Parameter<T> param)
          Return the value that has been bound to the parameter.
 java.lang.Object getParameterValue(java.lang.String name)
           
 java.util.List getResultList()
          Method to execute a (SELECT) query statement returning multiple results.
 X getSingleResult()
          Method to execute a SELECT statement returning a single result.
 java.util.Set<java.lang.String> getSupportedHints()
          Get the names of the hints that are supported for query objects.
 boolean isBound(javax.persistence.Parameter<?> param)
           
 JPAQuery<X> setFirstResult(int startPosition)
          Method to set the results to start from a particular position.
 JPAQuery<X> setFlushMode(javax.persistence.FlushModeType mode)
          Mutator for the flush mode.
 JPAQuery<X> setHint(java.lang.String hintName, java.lang.Object value)
          Method to add a vendor extension to the query.
 JPAQuery<X> setLockMode(javax.persistence.LockModeType lock)
           
 JPAQuery<X> setMaxResults(int max)
          Method to set the max number of results to return.
 JPAQuery<X> setParameter(int position, java.util.Calendar value, javax.persistence.TemporalType temporalType)
          Bind an instance of java.util.Calendar to a positional parameter.
 JPAQuery<X> setParameter(int position, java.util.Date value, javax.persistence.TemporalType temporalType)
          Bind an instance of java.util.Date to a positional parameter.
 JPAQuery<X> setParameter(int position, java.lang.Object value)
          Bind an argument to a positional parameter.
 JPAQuery<X> setParameter(javax.persistence.Parameter<java.util.Calendar> param, java.util.Calendar cal, javax.persistence.TemporalType type)
           
 JPAQuery<X> setParameter(javax.persistence.Parameter<java.util.Date> param, java.util.Date date, javax.persistence.TemporalType type)
           
<T> JPAQuery<X>
setParameter(javax.persistence.Parameter<T> param, T value)
          Bind the value of a Parameter object.
 JPAQuery<X> setParameter(java.lang.String name, java.util.Calendar value, javax.persistence.TemporalType temporalType)
          Bind an instance of java.util.Calendar to a named parameter.
 JPAQuery<X> setParameter(java.lang.String name, java.util.Date value, javax.persistence.TemporalType temporalType)
          Bind an instance of java.util.Date to a named parameter.
 JPAQuery<X> setParameter(java.lang.String name, java.lang.Object value)
          Bind an argument to a named parameter.
 java.lang.String toString()
          Method to return the single-string form of the query.
<T> T
unwrap(java.lang.Class<T> cls)
          Return an object of the specified type to allow access to the provider-specific API.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOCALISER

protected static final org.datanucleus.util.Localiser LOCALISER
Localisation utility for output messages

Constructor Detail

JPAQuery

public JPAQuery(javax.persistence.EntityManager em,
                org.datanucleus.store.query.Query query,
                java.lang.String language)
Constructor for a query used by JPA.

Parameters:
em - Entity Manager
query - Underlying query
language - Query language
Method Detail

executeUpdate

public int executeUpdate()
Method to execute a (UPDATE/DELETE) query returning the number of changed records.

Specified by:
executeUpdate in interface javax.persistence.Query
Returns:
Number of records updated/deleted with the query.
Throws:
javax.persistence.QueryTimeoutException - if the query times out

getResultList

public java.util.List getResultList()
Method to execute a (SELECT) query statement returning multiple results.

Specified by:
getResultList in interface javax.persistence.Query
Specified by:
getResultList in interface javax.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:
getSingleResult in interface javax.persistence.Query
Specified by:
getSingleResult in interface javax.persistence.TypedQuery<X>
Returns:
the result
Throws:
javax.persistence.QueryTimeoutException - if the query times out

setFirstResult

public JPAQuery<X> setFirstResult(int startPosition)
Method to set the results to start from a particular position.

Specified by:
setFirstResult in interface javax.persistence.Query
Specified by:
setFirstResult in interface javax.persistence.TypedQuery<X>
Parameters:
startPosition - position of first result numbered from 0
Returns:
The query

setMaxResults

public JPAQuery<X> setMaxResults(int max)
Method to set the max number of results to return.

Specified by:
setMaxResults in interface javax.persistence.Query
Specified by:
setMaxResults in interface javax.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:
getMaxResults in interface javax.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:
getFirstResult in interface javax.persistence.Query
Returns:
position of first result

setFlushMode

public JPAQuery<X> setFlushMode(javax.persistence.FlushModeType mode)
Mutator for the flush mode.

Specified by:
setFlushMode in interface javax.persistence.Query
Specified by:
setFlushMode in interface javax.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:
getFlushMode in interface javax.persistence.Query
Returns:
flush mode

setHint

public JPAQuery<X> setHint(java.lang.String hintName,
                           java.lang.Object value)
Method to add a vendor extension to the query. If the hint name is not recognized, it is silently ignored.

Specified by:
setHint in interface javax.persistence.Query
Specified by:
setHint in interface javax.persistence.TypedQuery<X>
Parameters:
hintName - Name of the "hint"
value - Value for the "hint"
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if the second argument is not valid for the implementation

getHints

public java.util.Map getHints()
Get the hints and associated values that are in effect for the query instance.

Specified by:
getHints in interface javax.persistence.Query
Returns:
query hints

getSupportedHints

public java.util.Set<java.lang.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> JPAQuery<X> setParameter(javax.persistence.Parameter<T> param,
                                    T value)
Bind the value of a Parameter object.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
param - parameter to be set
value - parameter value
Returns:
query instance
Throws:
java.lang.IllegalArgumentException - if parameter does not correspond to a parameter of the query

setParameter

public JPAQuery<X> setParameter(java.lang.String name,
                                java.lang.Object value)
Bind an argument to a named parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
name - the parameter name
value - The value for the param
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if parameter name does not correspond to parameter in query string or argument is of incorrect type

setParameter

public JPAQuery<X> setParameter(int position,
                                java.lang.Object value)
Bind an argument to a positional parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
position - Parameter position
value - The value
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if position does not correspond to positional parameter of query or argument is of incorrect type

setParameter

public JPAQuery<X> setParameter(java.lang.String name,
                                java.util.Date value,
                                javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Date to a named parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
name - Name of the param
value - Value for the param
temporalType - The temporal type
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if parameter name does not correspond to parameter in query string

setParameter

public JPAQuery<X> setParameter(java.lang.String name,
                                java.util.Calendar value,
                                javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Calendar to a named parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
name - name of the param
value - Value for the param
temporalType - The temporal type
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if parameter name does not correspond to parameter in query string

setParameter

public JPAQuery<X> setParameter(int position,
                                java.util.Date value,
                                javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Date to a positional parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
position - Parameter position
value - Value for the param
temporalType - Temporal Type
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if position does not correspond to positional parameter of query

setParameter

public JPAQuery<X> setParameter(int position,
                                java.util.Calendar value,
                                javax.persistence.TemporalType temporalType)
Bind an instance of java.util.Calendar to a positional parameter.

Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>
Parameters:
position - Parameter position
value - Value for the param
temporalType - Temporal type
Returns:
the same query instance
Throws:
java.lang.IllegalArgumentException - if position does not correspond to positional parameter of query

setParameter

public JPAQuery<X> setParameter(javax.persistence.Parameter<java.util.Calendar> param,
                                java.util.Calendar cal,
                                javax.persistence.TemporalType type)
Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>

setParameter

public JPAQuery<X> setParameter(javax.persistence.Parameter<java.util.Date> param,
                                java.util.Date date,
                                javax.persistence.TemporalType type)
Specified by:
setParameter in interface javax.persistence.Query
Specified by:
setParameter in interface javax.persistence.TypedQuery<X>

getInternalQuery

public org.datanucleus.store.query.Query getInternalQuery()
Accessor for the internal query.

Returns:
Internal query

unwrap

public <T> T unwrap(java.lang.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:
unwrap in interface javax.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 java.lang.String getLanguage()
Accessor for the query language.

Returns:
Query language

getParameters

public java.util.Set<javax.persistence.Parameter<?>> getParameters()
Get the query parameter objects. Returns empty set if the query has no parameters.

Specified by:
getParameters in interface javax.persistence.Query
Returns:
parameter objects

getParameter

public <T> javax.persistence.Parameter<T> getParameter(java.lang.String name,
                                                       java.lang.Class<T> type)
Get the parameter of the given name and type.

Specified by:
getParameter in interface javax.persistence.Query
Returns:
parameter object
Throws:
java.lang.IllegalArgumentException - if the parameter of the specified name and type doesn't exist

getParameter

public <T> javax.persistence.Parameter<T> getParameter(int position,
                                                       java.lang.Class<T> type)
Get the positional parameter with the given position and type.

Specified by:
getParameter in interface javax.persistence.Query
Returns:
parameter object
Throws:
java.lang.IllegalArgumentException - if the parameter with the specified position and type doesn't exist

getParameter

public javax.persistence.Parameter<?> getParameter(int position)
Specified by:
getParameter in interface javax.persistence.Query

getParameter

public javax.persistence.Parameter<?> getParameter(java.lang.String name)
Specified by:
getParameter in interface javax.persistence.Query

getParameterValue

public <T> T getParameterValue(javax.persistence.Parameter<T> param)
Return the value that has been bound to the parameter.

Specified by:
getParameterValue in interface javax.persistence.Query
Parameters:
param - parameter object
Returns:
parameter value
Throws:
java.lang.IllegalStateException - if the parameter has not been bound

getParameterValue

public java.lang.Object getParameterValue(int position)
Specified by:
getParameterValue in interface javax.persistence.Query

getParameterValue

public java.lang.Object getParameterValue(java.lang.String name)
Specified by:
getParameterValue in interface javax.persistence.Query

isBound

public boolean isBound(javax.persistence.Parameter<?> param)
Specified by:
isBound in interface javax.persistence.Query

getLockMode

public javax.persistence.LockModeType getLockMode()
Specified by:
getLockMode in interface javax.persistence.Query

setLockMode

public JPAQuery<X> setLockMode(javax.persistence.LockModeType lock)
Specified by:
setLockMode in interface javax.persistence.Query
Specified by:
setLockMode in interface javax.persistence.TypedQuery<X>

toString

public java.lang.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.

Overrides:
toString in class java.lang.Object
Returns:
The single-string form of the query


Copyright © 2011. All Rights Reserved.