|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.nuiton.topia.framework.TopiaQuery
public class TopiaQuery
Query HQL managment to simplify usage of
TopiaContext.find(String, 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(String, 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(String, 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<? extends TopiaEntity> |
dao
|
protected boolean |
distinct
|
protected Integer |
endIndex
|
protected StringBuilder |
from
From part of the query |
protected StringBuilder |
groupBy
Group By part of the query |
protected String |
mainAlias
|
protected StringBuilder |
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 statement |
protected List<String> |
propertiesToLoad
|
protected StringBuilder |
select
Select part of the query |
protected Integer |
startIndex
|
protected StringBuilder |
where
Where part of the query |
| Constructor Summary | |
|---|---|
TopiaQuery()
|
|
TopiaQuery(Class<? extends TopiaEntity> mainEntityClass)
Create a TopiaQuery based on the entityClass. |
|
TopiaQuery(Class<? extends TopiaEntity> mainEntityClass,
String alias)
Create a TopiaQuery based on the entityClass. |
|
TopiaQuery(TopiaDAO<? extends TopiaEntity> dao)
Create a TopiaQuery from a DAO. |
|
TopiaQuery(TopiaDAO<? extends TopiaEntity> dao,
String alias)
Create a TopiaQuery from a DAO with an Alias. |
|
| Method Summary | ||
|---|---|---|
TopiaQuery |
add(Map<String,Object> properties)
Add a map of properties to the where clause of the query. |
|
TopiaQuery |
add(String where)
Add a where element to the Query. |
|
TopiaQuery |
add(String paramName,
Collection<Object> values,
boolean isNull)
Deprecated. use add(String, Object...)
with a null or not |
|
TopiaQuery |
add(String paramName,
Object... paramValue)
Add an element to the query. |
|
TopiaQuery |
add(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
Add an element to the query. |
|
TopiaQuery |
addDistinct()
Add the distinct key word in the query. |
|
TopiaQuery |
addFrom(Class<? extends TopiaEntity> entityClass)
Add an other entity type to the from in the query. |
|
TopiaQuery |
addFrom(Class<? extends TopiaEntity> entityClass,
String alias)
Add an other entity type to the from in the query with an alias. |
|
TopiaQuery |
addFrom(String str)
Add an element to the from in the query. |
|
TopiaQuery |
addGroup(String... group)
Add an element to the group of the query. |
|
TopiaQuery |
addLoad(String... properties)
Add a property to load when query is executed. |
|
TopiaQuery |
addNotNull(String paramName)
Add an element to the query with the constraint Not null. |
|
TopiaQuery |
addNullOr(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
Add an element to the query. |
|
TopiaQuery |
addOrder(String... order)
Add an element to the order in the query. |
|
TopiaQuery |
addOrderDesc(String order)
|
|
TopiaQuery |
addParam(String id,
Object paramValue)
Add a HQL parameter to the Query. |
|
TopiaQuery |
addParams(List<Object> params)
Add muliple paramaters to the Query. |
|
TopiaQuery |
addSelect(String... select)
Add an element to the select in the query. |
|
protected String |
convertStringArray(String... array)
Helper method for array type. |
|
static
|
createQuery(Class<T> entityClass)
Deprecated. use constructor instead : TopiaQuery(TopiaDAO) |
|
static
|
createQuery(Class<T> entityClass,
String alias)
Deprecated. use constructor instead : TopiaQuery(Class, String) |
|
static
|
createQuery(TopiaDAO<T> dao)
Deprecated. use constructor instead : TopiaQuery(TopiaDAO) |
|
static
|
createQuery(TopiaDAO<T> dao,
String alias)
Deprecated. use constructor instead : java.lang.Class) |
|
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. |
|
|
executeToEntity()
Deprecated. use dao method instead : TopiaDAO.findByQuery(TopiaQuery) |
|
|
executeToEntity(TopiaContext transaction,
Class<E> entityClass)
Execute the query and get the first result entity. |
|
|
executeToEntityList()
Deprecated. use dao method instead : TopiaDAO.findAllByQuery(TopiaQuery) |
|
|
executeToEntityList(TopiaContext transaction,
Class<E> entityClass)
Execute the query and get a List of entity. |
|
|
executeToEntityMap()
Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery) |
|
|
executeToEntityMap(String keyName,
Class<K> keyClass)
Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery, String, Class) |
|
|
executeToEntityMap(TopiaContext transaction,
Class<E> entityClass)
Execute the query and get a Map of entity with topiaId in key. |
|
|
executeToEntityMap(TopiaContext transaction,
Class<E> entityClass,
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. |
|
protected void |
finalize()
|
|
String |
fullQuery()
Get the full query. |
|
String |
getMainAlias()
Return the mainAlias set from constructor. |
|
List<Object> |
getParams()
|
|
protected List<String> |
getPropertiesToLoad()
|
|
static String |
getProperty(String... entityProperty)
This method is used to concat properties from entities. |
|
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. |
|
TopiaQuery |
resetLimit()
Remove limits previously set |
|
TopiaQuery |
setFrom(Class<? extends TopiaEntity> mainEntityClass)
Set the mainEntity in the from part of the query. |
|
TopiaQuery |
setFrom(Class<? extends TopiaEntity> mainEntityClass,
String alias)
Set the mainEntity in the from part of the query and use an alias for this mainEntity. |
|
TopiaQuery |
setLimit(int start,
int end)
Limit the result of the query with startIndex and endIndex. |
|
TopiaQuery |
setMaxResults(int max)
Set the max results wanted for the query. |
|
TopiaQuery |
setSelect(String... select)
Set the select in the query. |
|
String |
toString()
|
|
protected boolean |
validateDAO()
|
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected List<Object> params
protected StringBuilder select
protected boolean distinct
protected StringBuilder from
protected StringBuilder where
protected StringBuilder orderBy
protected StringBuilder groupBy
protected Integer startIndex
protected Integer endIndex
protected boolean parentheses
protected List<String> propertiesToLoad
protected String mainAlias
protected TopiaDAO<? extends TopiaEntity> dao
| Constructor Detail |
|---|
public TopiaQuery()
public TopiaQuery(Class<? extends TopiaEntity> mainEntityClass)
entityClass. The from statement
is automatically set.
mainEntityClass -
public TopiaQuery(Class<? extends TopiaEntity> mainEntityClass,
String alias)
entityClass. The from statement
is automatically set, the select statement must be necessary in some
case, the query will manage this case using the mainAlias by default.
mainEntityClass - alias - for the mainEntityClasspublic TopiaQuery(TopiaDAO<? extends TopiaEntity> dao)
dao - DAO linked to the entity to threat
public TopiaQuery(TopiaDAO<? extends TopiaEntity> dao,
String alias)
dao - DAO linked to the entity to threatalias - of the main entity in the query| Method Detail |
|---|
public TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass)
mainEntityClass - type of the mainEntity
public TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass,
String alias)
mainEntityClass - type of the mainEntityalias - for the entity in the query
public TopiaQuery addFrom(String str)
str - the element to add
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass)
entityClass - different from the mainEntity in the DAO
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass,
String alias)
entityClass - different from the mainEntity in the DAOalias - of the entity in the query
@Deprecated public static <T extends TopiaEntity> TopiaQuery createQuery(Class<T> entityClass)
TopiaQuery(TopiaDAO)
T - entity type extends TopiaEntityentityClass - Class for an entity Query
@Deprecated public static <T extends TopiaEntity> TopiaQuery createQuery(TopiaDAO<T> dao)
TopiaQuery(TopiaDAO)
T - entity type in the dao extends TopiaEntitydao - DAO linked to the entity to threat
@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(Class<T> entityClass,
String alias)
TopiaQuery(Class, String)
T - entity type in the dao extends TopiaEntityentityClass - Class for an entity Queryalias - of the main entity in the query
@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(TopiaDAO<T> dao,
String alias)
java.lang.Class)
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 addParam(String id,
Object paramValue)
id - identification of the param in the queryparamValue - value of the param
public TopiaQuery addParams(List<Object> params)
params - a list of HQL params with key and value in order.
getValueName(String)public List<Object> getParams()
public String getMainAlias()
public TopiaQuery 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 add(String where)
where - element to add
public TopiaQuery 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,
...)
public TopiaQuery addNullOr(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
paramName - the name of the parameter in the query (attribute of
the entity)constraint - the operation concerned by the orparamValue - the value of the parameter (an other entity, a String,
...)
protected String getValueName(String paramName)
public TopiaQuery addNotNull(String paramName)
paramName - name of the parameter in the query
public TopiaQuery add(String paramName,
Object... paramValue)
paramValue list (except if it's
the only one value, it's ambiguous).
Note : this method do nothing if the paramValue is not defined.
If you want to add the constraint null on the paramName use
add(String, TopiaQuery.Op, Object) as :
add("param", Op.EQ, null);
paramName - name of the parameter in the queryparamValue - values of the parameter
add(String, Op, Object)
@Deprecated
public TopiaQuery add(String paramName,
Collection<Object> values,
boolean isNull)
add(String, Object...)
with a null or not
paramName - name of the parameter in the queryvalues - different values for this parameterisNull - use it to test the nullity of parameter
add(String)public TopiaQuery add(Map<String,Object> properties)
properties -
public TopiaQuery addSelect(String... select)
setSelect(String...)
method instead.
select - element to add
public TopiaQuery setSelect(String... select)
select - element to set
public TopiaQuery addDistinct()
public TopiaQuery addOrder(String... order)
order - element to add
public TopiaQuery addOrderDesc(String order)
public TopiaQuery addGroup(String... group)
group - element to add
protected String convertStringArray(String... array)
array - of String
public TopiaQuery setLimit(int start,
int end)
start - first index to get from the resultsend - last index to get from the results
public TopiaQuery resetLimit()
public TopiaQuery 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(String, Object...)
public List execute()
throws TopiaException
TopiaExceptionexecute(TopiaContext)
public <E extends TopiaEntity> List<E> executeToEntityList(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - transaction - the TopiaContext to use for executionentityClass -
TopiaException
ClassCastException
@Deprecated
public <E extends TopiaEntity> List<E> executeToEntityList()
throws TopiaException,
ClassCastException
TopiaDAO.findAllByQuery(TopiaQuery)
E -
TopiaException
ClassCastExceptionexecuteToEntityList(TopiaContext,Class)
public <E extends TopiaEntity,K> Map<K,E> executeToEntityMap(TopiaContext transaction,
Class<E> entityClass,
String keyName,
Class<K> keyClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - K - the type of the map keytransaction - the TopiaContext to use for executionentityClass - keyName - the property name of the key in the entitykeyClass - the key class for the result map
TopiaException
ClassCastException
@Deprecated
public <E extends TopiaEntity,K> Map<K,E> executeToEntityMap(String keyName,
Class<K> keyClass)
throws TopiaException,
ClassCastException
TopiaDAO.findAllMappedByQuery(TopiaQuery, String, Class)
E - K - keyName - keyClass -
TopiaException
ClassCastExceptionexecuteToEntityMap(TopiaContext, Class)
public <E extends TopiaEntity> Map<String,E> executeToEntityMap(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - transaction - the TopiaContext to use for executionentityClass -
TopiaException
ClassCastException
@Deprecated
public <E extends TopiaEntity> Map<String,E> executeToEntityMap()
throws TopiaException,
ClassCastException
TopiaDAO.findAllMappedByQuery(TopiaQuery)
E -
TopiaException
ClassCastExceptionexecuteToEntityMap(TopiaContext,Class)
public <E extends TopiaEntity> E executeToEntity(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - transaction - the TopiaContext to use for executionentityClass -
TopiaException
ClassCastException
@Deprecated
public <E extends TopiaEntity> E executeToEntity()
throws TopiaException,
ClassCastException
TopiaDAO.findByQuery(TopiaQuery)
E -
TopiaException
ClassCastExceptionexecuteToEntity(TopiaContext,Class)
public Object executeToObject(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden
TopiaException
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(TopiaContext, 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(TopiaContext, String)
public Object executeToObject(String select)
throws TopiaException
select -
TopiaExceptionexecuteToObject(TopiaContext, String)
public int executeCount(TopiaContext transaction)
throws TopiaException
transaction - the TopiaContext to use for execution
TopiaException
public int executeCount()
throws TopiaException
TopiaExceptionexecuteCount(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
TopiaExceptionprotected void finalize()
finalize in class Objectpublic static String getProperty(String... entityProperty)
entityProperty - to concat
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||