ch.elca.el4j.services.persistence.jpa.util
Class JpaQuery<T>

java.lang.Object
  extended by ch.elca.el4j.services.persistence.jpa.util.JpaQuery<T>
Type Parameters:
T - The type parameter.

public class JpaQuery<T>
extends Object

Query object returned by DataService.

Author:
David Bernhard (dab)
File-location:
JpaQuery
Last check-in date:
2010-08-05 11:00:36 +0200 (Do, 05. Aug 2010) by swrelca for revision 4112

Nested Class Summary
static class JpaQuery.Order
          Order of a sort.
static class JpaQuery.Relation
          The possible relations (eq, neq ...).
 
Constructor Summary
JpaQuery(Class<T> cls, JpaHelperImpl ds)
          Create the query object.
 
Method Summary
 JpaQuery<T> detach()
          Detach all returned elements.
 List<T> execute()
          Execute the query.
 List<T> executeHQL(String query, Object... params)
          Execute a HQL query.
 T executeHQLUnique(String query, Object... params)
          Execute HQL and epxect a single item.
 T executeUnique()
          Execute the query, expecting a unique element.
 JpaQuery<T> extent(String... names)
          Add a data extent (a property to be eagerly fetched).
 JpaQuery<T> failOnNull()
          Fail if no elements are returned.
 JpaQuery<T> order(String key, JpaQuery.Order order)
          Add an order.
 JpaQuery<T> where(Criterion c)
          Add a hibernate criterion.
 JpaQuery<T> where(String key, JpaQuery.Relation r, Object value)
          Add a restriction.
 JpaQuery<T> where(String key, Object value)
          Add a restriction.
 JpaQuery<T> whereNotNull(String key)
          Add an is--not-null restriction.
 JpaQuery<T> whereNull(String key)
          Add an is-null restriction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaQuery

public JpaQuery(Class<T> cls,
                JpaHelperImpl ds)
Create the query object.

Parameters:
cls - The class to query for.
context - The context.
ds - The data service.
Method Detail

where

public JpaQuery<T> where(String key,
                         Object value)
Add a restriction.

Parameters:
key - The property name.
value - The property value.
Returns:
this

where

public JpaQuery<T> where(Criterion c)
Add a hibernate criterion.

Parameters:
c - The criterion.
Returns:
this

where

public JpaQuery<T> where(String key,
                         JpaQuery.Relation r,
                         Object value)
Add a restriction.

Parameters:
key - The property name.
r - The relation type.
value - The property value.
Returns:
this

whereNull

public JpaQuery<T> whereNull(String key)
Add an is-null restriction.

Parameters:
key - The property name.
Returns:
this

whereNotNull

public JpaQuery<T> whereNotNull(String key)
Add an is--not-null restriction.

Parameters:
key - The property name.
Returns:
this

order

public JpaQuery<T> order(String key,
                         JpaQuery.Order order)
Add an order.

Parameters:
key - The property to order by.
order - The order to use.
Returns:
this

extent

public JpaQuery<T> extent(String... names)
Add a data extent (a property to be eagerly fetched).

Parameters:
names - The names of the properties to fetch.
Returns:
this

failOnNull

public JpaQuery<T> failOnNull()
Fail if no elements are returned.

Returns:
this.

detach

public JpaQuery<T> detach()
Detach all returned elements.

Returns:
this.

execute

public List<T> execute()
Execute the query.

Returns:
The query result.

executeUnique

public T executeUnique()
Execute the query, expecting a unique element. If none is found, return null. If several are found, throw an exception.

Returns:
The unique element matching the criteria.

executeHQL

public List<T> executeHQL(String query,
                          Object... params)
Execute a HQL query. Warning: You must pass "distinct" yourself if desired. Due to the nature of HQL, this is not fully typesafe - types are checked but we cannot prevent an invalid tpye in the "from" clause.

Parameters:
query - The query string.
params - The parameters.
Returns:
The single object, if present.

executeHQLUnique

public T executeHQLUnique(String query,
                          Object... params)
Execute HQL and epxect a single item. Throw an exception if more than one is found. Warning: You must pass "distinct" yourself if desired. Due to the nature of HQL, this is not fully typesafe - types are checked but we cannot prevent an invalid tpye in the "from" clause.

Parameters:
query - The query string.
params - The parameters.
Returns:
The single object, if present.


Copyright © 2005-2011 ELCA. All Rights Reserved.