|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.nuiton.topia.framework.TopiaQuery<E>
E - The main entity for the querypublic class TopiaQuery<E extends TopiaEntity>
Query HQL managment to simplify usage of
TopiaContext.find(java.lang.String, java.lang.Object[]).
TODO-FD20091224 Complete documentation of this class + JUnit Tests
This class is used to construct a HQL query and then execute it from a
TopiaContext. The TopiaQuery is linked to a TopiaEntity which
is the main element manipulated in the query. There is two parts in using
this class :
- construction of the query, using add, addFrom, addOrder, addSelect,
addGroup, ...
- execution of the query, using executeToEntityList, executeToEntity,
executeToInteger, ...
Construction
============
This class make easier the way to construct a HQL query.
Example 1 :
-----------
SQL :
"SELECT * FROM PersonImpl WHERE firstName LIKE 'M%' AND year > 1980"
HQL using TopiaContext.find(java.lang.String, java.lang.Object[]) :
TopiaContext context = rootContext.beginTransaction();
context.find("FROM " + Person.class.getName() + " WHERE firstName LIKE :firstName AND year > :year",
"firstName", "M%", year, 1980);
TopiaQuery :
TopiaQuery query = TopiaQuery.createQuery(Person.class).add(
Person.FIRST_NAME, Op.LIKE, "M%").add(Person.YEAR, Op.GT, 1980);
But the real advantage is when you have some parameters to test before adding
them to the query. With the older method, it was tidious to construct
and add parameters to finally use the find method from TopiaContext.
Example 2 :
-----------
HQL using TopiaContext.find(java.lang.String, java.lang.Object[]) :
TopiaContext context = rootContext.beginTransaction();
String query = "FROM " + Person.class.getName();
List
Created: 21 déc. 2009
| Nested Class Summary | |
|---|---|
static class |
TopiaQuery.Op
Enum to simmplify using operation in query |
| Field Summary | |
|---|---|
protected TopiaDAO<E> |
dao
|
protected boolean |
distinct
|
protected Integer |
endIndex
|
protected String |
from
From part of the query |
protected String |
groupBy
Group By part of the query |
protected String |
mainAlias
|
protected Class<E> |
mainEntityClass
|
protected String |
orderBy
Order By part of the query |
protected List<Object> |
params
Params for HQL query |
protected boolean |
parentheses
Used to determine if parentheses are needed for Where input |
protected List<String> |
propertiesToLoad
|
protected String |
select
Select part of the query |
protected Integer |
startIndex
|
protected String |
where
Where part of the query |
| Constructor Summary | |
|---|---|
protected |
TopiaQuery(Class<E> entityClass)
|
protected |
TopiaQuery(Class<E> entityClass,
String alias)
|
protected |
TopiaQuery(TopiaDAO<E> dao)
|
protected |
TopiaQuery(TopiaDAO<E> dao,
String alias)
|
| Method Summary | ||
|---|---|---|
TopiaQuery<E> |
add(Map<String,Object> properties)
Add a map of properties to the where clause of the query. |
|
TopiaQuery<E> |
add(String where)
Add a where element to the Query. |
|
TopiaQuery<E> |
add(String paramName,
Collection<Object> values)
Add an element to the query with a list of different possible values. |
|
TopiaQuery<E> |
add(String paramName,
Collection<Object> values,
boolean isNull)
Add an element to the query with a list of different values. |
|
TopiaQuery<E> |
add(String paramName,
Object paramValue)
Add an element to the query. |
|
TopiaQuery<E> |
add(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
Add an element to the query. |
|
TopiaQuery<E> |
addDistinct()
Add the distinct key word in the query. |
|
TopiaQuery<E> |
addFrom(String str)
Add an element to the from in the query. |
|
TopiaQuery<E> |
addGroup(String group)
Add an element to the group of the query. |
|
TopiaQuery<E> |
addLoad(String... properties)
Add a property to load when query is executed. |
|
TopiaQuery<E> |
addNotNull(String paramName)
Add an element to the query with the constraint Not null. |
|
TopiaQuery<E> |
addOrder(String order)
Add an element to the order in the query. |
|
TopiaQuery<E> |
addOrderDesc(String order)
|
|
TopiaQuery<E> |
addParam(String id,
Object paramValue)
Add a HQL parameter to the Query. |
|
TopiaQuery<E> |
addParams(List<Object> params)
Add muliple paramaters to the Query. |
|
TopiaQuery<E> |
addSelect(String select)
Add an element to the select in the query. |
|
static
|
createQuery(Class<T> entityClass)
Create a TopiaQuery with entityClass initialization. |
|
static
|
createQuery(Class<T> entityClass,
String alias)
Create a TopiaQuery with entityClass initialization and its Alias. |
|
static
|
createQuery(TopiaDAO<T> dao)
Create a TopiaQuery from a DAO. |
|
static
|
createQuery(TopiaDAO<T> dao,
String alias)
Create a TopiaQuery from a DAO with an Alias. |
|
List |
execute()
DAO must be defined to use this method. |
|
List |
execute(TopiaContext transaction)
Simple execution of the query. |
|
int |
executeCount()
DAO must be defined to use this method. |
|
int |
executeCount(TopiaContext transaction)
Execute a simple count on the query, i.e. the number of results get from the query. |
|
E |
executeToEntity()
DAO must be defined to use this method. |
|
E |
executeToEntity(TopiaContext transaction)
Execute the query and get the first result entity. |
|
List<E> |
executeToEntityList()
DAO must be defined to use this method. |
|
List<E> |
executeToEntityList(TopiaContext transaction)
Execute the query and get a List of entity. |
|
Map<String,E> |
executeToEntityMap()
DAO must be defined to use this method. |
|
|
executeToEntityMap(String keyName,
Class<K> keyClass)
DAO must be defined to use this method. |
|
Map<String,E> |
executeToEntityMap(TopiaContext transaction)
Execute the query and get a Map of entity with topiaId in key. |
|
|
executeToEntityMap(TopiaContext transaction,
String keyName,
Class<K> keyClass)
Execute the query and get a Map of entity with key type in argument. |
|
int |
executeToInteger(String select)
DAO must be defined to use this method. |
|
int |
executeToInteger(TopiaContext transaction,
String select)
Execute the query and get an Integer for result. |
|
Object |
executeToObject(String select)
DAO must be defined to use this method. |
|
Object |
executeToObject(TopiaContext transaction,
String select)
Execute the query and get an Object for result. |
|
String |
executeToString(String select)
DAO must be defined to use this method. |
|
String |
executeToString(TopiaContext transaction,
String select)
Execute the query and get a String for result. |
|
String |
fullQuery()
Get the full query. |
|
String |
getMainAlias()
Return the mainAlias set from constructor. |
|
List<Object> |
getParams()
|
|
protected List<String> |
getPropertiesToLoad()
|
|
protected String |
getValueName(String paramName)
|
|
protected
|
loadEntityProperty(T entity,
String property)
Load a property of type TopiaEntity from an other entity. |
|
protected
|
loadProperties(T entity)
Load all properties for the entity. |
|
protected
|
loadProperty(T entity,
String property)
Load a property from an entity. |
|
protected void |
resetLimit()
|
|
TopiaQuery<E> |
setLimit(int start,
int end)
Limit the result of the query with startIndex and endIndex. |
|
TopiaQuery<E> |
setMaxResults(int max)
Set the max results wanted for the query. |
|
TopiaQuery<E> |
setSelect(String select)
Set the select in the query. |
|
String |
toString()
|
|
protected boolean |
validateDAO()
|
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected List<Object> params
protected String select
protected boolean distinct
protected String from
protected String where
protected String orderBy
protected String groupBy
protected Integer startIndex
protected Integer endIndex
protected boolean parentheses
protected List<String> propertiesToLoad
protected Class<E extends TopiaEntity> mainEntityClass
protected String mainAlias
protected TopiaDAO<E extends TopiaEntity> dao
| Constructor Detail |
|---|
protected TopiaQuery(Class<E> entityClass)
protected TopiaQuery(TopiaDAO<E> dao)
protected TopiaQuery(Class<E> entityClass,
String alias)
protected TopiaQuery(TopiaDAO<E> dao,
String alias)
| Method Detail |
|---|
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(Class<T> entityClass)
T - entity type extends TopiaEntityentityClass - Class for an entity Query
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao)
T - entity type in the dao extends TopiaEntitydao - DAO linked to the entity to threat
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(Class<T> entityClass,
String alias)
T - entity type in the dao extends TopiaEntityentityClass - Class for an entity Queryalias - of the main entity in the query
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao,
String alias)
T - entity type in the dao extends TopiaEntitydao - DAO linked to the entity to threatalias - of the main entity in the query
public String toString()
toString in class Objectpublic String fullQuery()
public TopiaQuery<E> addParam(String id,
Object paramValue)
id - identification of the param in the queryparamValue - value of the param
public TopiaQuery<E> addParams(List<Object> params)
params - a list of HQL params with key and value in order.
getValueName(java.lang.String)public List<Object> getParams()
public String getMainAlias()
public TopiaQuery<E> addLoad(String... properties)
Exemples : - "person.company" (Property TopiaEntity person linked to the result entity in query and company linked to person) --> calling myEntity.getPerson().getCompany(); - "partyRoles" (Property Collection partyRoles linked to the result entity in query) --> calling myEntity.getPartyRoles().size();
properties -
protected List<String> getPropertiesToLoad()
public TopiaQuery<E> add(String where)
where - element to add
public TopiaQuery<E> add(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
paramName - the name of the parameter in the query (attribute of
the entity)constraint - the operation concernedparamValue - the value of the parameter (an other entity, a String, ...)
protected String getValueName(String paramName)
public TopiaQuery<E> addNotNull(String paramName)
paramName - name of the parameter in the query
public TopiaQuery<E> add(String paramName,
Object paramValue)
paramName - name of the parameter in the queryparamValue - value of the parameter
add(String, TopiaQuery.Op, Object)
public TopiaQuery<E> add(String paramName,
Collection<Object> values)
paramName - name of the parameter in the queryvalues - different values for this parameter
add(java.lang.String, java.util.Collection, boolean)
public TopiaQuery<E> add(String paramName,
Collection<Object> values,
boolean isNull)
paramName - name of the parameter in the queryvalues - different values for this parameterisNull - use it to test the nullity of parameter
add(java.lang.String)public TopiaQuery<E> add(Map<String,Object> properties)
properties -
public TopiaQuery<E> addFrom(String str)
str - the element to add
public TopiaQuery<E> addSelect(String select)
setSelect(java.lang.String)
method instead.
select - element to add
public TopiaQuery<E> setSelect(String select)
select - element to set
public TopiaQuery<E> addDistinct()
public TopiaQuery<E> addOrder(String order)
order - element to add
public TopiaQuery<E> addOrderDesc(String order)
public TopiaQuery<E> addGroup(String group)
group - element to add
public TopiaQuery<E> setLimit(int start,
int end)
start - first index to get from the resultsend - last index to get from the results
protected void resetLimit()
public TopiaQuery<E> setMaxResults(int max)
max - the number of elements wanted
public List execute(TopiaContext transaction)
throws TopiaException
transaction - the TopiaContext to use for execution
TopiaExceptionTopiaContext.find(java.lang.String, java.lang.Object[])
public List execute()
throws TopiaException
TopiaExceptionexecute(org.nuiton.topia.TopiaContext)
public List<E> executeToEntityList(TopiaContext transaction)
throws TopiaException,
ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
ClassCastException
public List<E> executeToEntityList()
throws TopiaException,
ClassCastException
TopiaException
ClassCastExceptionexecuteToEntityList(org.nuiton.topia.TopiaContext)
public <K> Map<K,E> executeToEntityMap(TopiaContext transaction,
String keyName,
Class<K> keyClass)
throws TopiaException,
ClassCastException
addLoad(java.lang.String[]).
K - the type of the map keytransaction - the TopiaContext to use for executionkeyName - the property name of the key in the entitykeyClass - the key class for the result map
TopiaException
ClassCastException
public <K> Map<K,E> executeToEntityMap(String keyName,
Class<K> keyClass)
throws TopiaException,
ClassCastException
K - keyName - keyClass -
TopiaException
ClassCastExceptionexecuteToEntityMap(org.nuiton.topia.TopiaContext, java.lang.String, java.lang.Class)
public Map<String,E> executeToEntityMap(TopiaContext transaction)
throws TopiaException,
ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
ClassCastException
public Map<String,E> executeToEntityMap()
throws TopiaException,
ClassCastException
TopiaException
ClassCastExceptionexecuteToEntityMap(org.nuiton.topia.TopiaContext)
public E executeToEntity(TopiaContext transaction)
throws TopiaException,
ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
ClassCastException
public E executeToEntity()
throws TopiaException,
ClassCastException
TopiaException
ClassCastExceptionexecuteToEntity(org.nuiton.topia.TopiaContext)
public int executeToInteger(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : SUM(myParam))
TopiaException
public int executeToInteger(String select)
throws TopiaException
select -
TopiaExceptionexecuteToInteger(org.nuiton.topia.TopiaContext, java.lang.String)
public String executeToString(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : MAX(myParam))
TopiaException
public String executeToString(String select)
throws TopiaException
select -
TopiaExceptionexecuteToString(org.nuiton.topia.TopiaContext, java.lang.String)
public Object executeToObject(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden
TopiaException
public Object executeToObject(String select)
throws TopiaException
select -
TopiaExceptionexecuteToObject(org.nuiton.topia.TopiaContext, java.lang.String)
public int executeCount(TopiaContext transaction)
throws TopiaException
transaction - the TopiaContext to use for execution
TopiaException
public int executeCount()
throws TopiaException
TopiaExceptionexecuteCount(org.nuiton.topia.TopiaContext)
protected boolean validateDAO()
throws TopiaException
TopiaException
protected <T extends TopiaEntity> void loadProperties(T entity)
throws TopiaException
T - type of the entity extends TopiaEntityentity - used to load properties
TopiaException
protected <T extends TopiaEntity> TopiaEntity loadEntityProperty(T entity,
String property)
throws TopiaException
T - type of the entity extends TopiaEntityentity - used to load the propertyproperty - name of the property in the entity
TopiaException
protected <T extends TopiaEntity> Object loadProperty(T entity,
String property)
throws TopiaException
T - type of the entity extends TopiaEntityentity - used to load the propertyproperty - name of the property in the entity
TopiaException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||