|
||||||||||
| 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 java.lang.Integer |
endIndex
|
protected java.lang.String |
from
From part of the query |
protected java.lang.String |
groupBy
Group By part of the query |
protected java.lang.String |
mainAlias
|
protected java.lang.Class<E> |
mainEntityClass
|
protected java.lang.String |
orderBy
Order By part of the query |
protected java.util.List<java.lang.Object> |
params
Params for HQL query |
protected boolean |
parentheses
Used to determine if parentheses are needed for Where input |
protected java.util.List<java.lang.String> |
propertiesToLoad
|
protected java.lang.String |
select
Select part of the query |
protected java.lang.Integer |
startIndex
|
protected java.lang.String |
where
Where part of the query |
| Constructor Summary | |
|---|---|
protected |
TopiaQuery(java.lang.Class<E> entityClass)
Constructor of TopiaQuery with entityClass initialization. |
protected |
TopiaQuery(java.lang.Class<E> entityClass,
java.lang.String alias)
Constructor of TopiaQuery with String form initialization. |
protected |
TopiaQuery(TopiaDAO<E> dao)
Constructor of TopiaQuery from a DAO |
protected |
TopiaQuery(TopiaDAO<E> dao,
java.lang.String alias)
Constructor of TopiaQuery from a DAO with an Alias |
| Method Summary | ||
|---|---|---|
TopiaQuery<E> |
add(java.util.Map<java.lang.String,java.lang.Object> properties)
Add a map of properties to the where clause of the query. |
|
TopiaQuery<E> |
add(java.lang.String where)
Add a where element to the Query. |
|
TopiaQuery<E> |
add(java.lang.String paramName,
java.util.Collection<java.lang.Object> values)
Add an element to the query with a list of different possible values. |
|
TopiaQuery<E> |
add(java.lang.String paramName,
java.util.Collection<java.lang.Object> values,
boolean isNull)
Add an element to the query with a list of different values. |
|
TopiaQuery<E> |
add(java.lang.String paramName,
java.lang.Object paramValue)
Add an element to the query. |
|
TopiaQuery<E> |
add(java.lang.String paramName,
TopiaQuery.Op constraint,
java.lang.Object paramValue)
Add an element to the query. |
|
TopiaQuery<E> |
addDistinct()
Add the distinct key word in the query. |
|
TopiaQuery<E> |
addFrom(java.lang.String str)
Add an element to the from in the query. |
|
TopiaQuery<E> |
addGroup(java.lang.String group)
Add an element to the group of the query. |
|
TopiaQuery<E> |
addLoad(java.lang.String... properties)
Add a property to load when query is executed. |
|
TopiaQuery<E> |
addNotNull(java.lang.String paramName)
Add an element to the query with the constraint Not null. |
|
TopiaQuery<E> |
addOrder(java.lang.String order)
Add an element to the order in the query. |
|
TopiaQuery<E> |
addOrderDesc(java.lang.String order)
|
|
TopiaQuery<E> |
addParam(java.lang.String id,
java.lang.Object e)
Add a HQL parameter to the Query. |
|
static
|
createQuery(java.lang.Class<T> entityClass)
|
|
static
|
createQuery(java.lang.Class<T> entityClass,
java.lang.String alias)
|
|
static
|
createQuery(TopiaDAO<T> dao)
|
|
static
|
createQuery(TopiaDAO<T> dao,
java.lang.String alias)
|
|
java.util.List |
execute()
DAO must be defined to use this method. |
|
java.util.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. |
|
java.util.List<E> |
executeToEntityList()
DAO must be defined to use this method. |
|
java.util.List<E> |
executeToEntityList(TopiaContext transaction)
Execute the query and get a List of entity. |
|
java.util.Map<java.lang.String,E> |
executeToEntityMap()
DAO must be defined to use this method. |
|
|
executeToEntityMap(java.lang.String keyName,
java.lang.Class<K> keyClass)
DAO must be defined to use this method. |
|
java.util.Map<java.lang.String,E> |
executeToEntityMap(TopiaContext transaction)
Execute the query and get a Map of entity with topiaId in key. |
|
|
executeToEntityMap(TopiaContext transaction,
java.lang.String keyName,
java.lang.Class<K> keyClass)
Execute the query and get a Map of entity with key type in argument. |
|
int |
executeToInteger(java.lang.String select)
DAO must be defined to use this method. |
|
int |
executeToInteger(TopiaContext transaction,
java.lang.String select)
Execute the query and get an Integer for result. |
|
java.lang.Object |
executeToObject(java.lang.String select)
DAO must be defined to use this method. |
|
java.lang.Object |
executeToObject(TopiaContext transaction,
java.lang.String select)
Execute the query and get an Object for result. |
|
java.lang.String |
executeToString(java.lang.String select)
DAO must be defined to use this method. |
|
java.lang.String |
executeToString(TopiaContext transaction,
java.lang.String select)
Execute the query and get a String for result. |
|
java.lang.String |
fullQuery()
Get the full query. |
|
protected java.util.List<java.lang.Object> |
getParams()
|
|
protected java.util.List<java.lang.String> |
getPropertiesToLoad()
|
|
protected java.lang.String |
getValueName(java.lang.String paramName)
|
|
protected
|
loadEntityProperty(T entity,
java.lang.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,
java.lang.String property)
Load a property from an entity. |
|
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(java.lang.String select)
Set the select in the query. |
|
java.lang.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 java.util.List<java.lang.Object> params
protected java.lang.String select
protected boolean distinct
protected java.lang.String from
protected java.lang.String where
protected java.lang.String orderBy
protected java.lang.String groupBy
protected java.lang.Integer startIndex
protected java.lang.Integer endIndex
protected boolean parentheses
protected java.util.List<java.lang.String> propertiesToLoad
protected java.lang.Class<E extends TopiaEntity> mainEntityClass
protected java.lang.String mainAlias
protected TopiaDAO<E extends TopiaEntity> dao
| Constructor Detail |
|---|
protected TopiaQuery(java.lang.Class<E> entityClass)
entityClass - Class for an entity Queryprotected TopiaQuery(TopiaDAO<E> dao)
dao - DAO linked to the entity to threat
protected TopiaQuery(java.lang.Class<E> entityClass,
java.lang.String alias)
entityClass - alias - of the main entity in the query
protected TopiaQuery(TopiaDAO<E> dao,
java.lang.String alias)
dao - DAO linked to the entity to threatalias - of the main entity in the query| Method Detail |
|---|
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(java.lang.Class<T> entityClass)
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao)
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(java.lang.Class<T> entityClass,
java.lang.String alias)
public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao,
java.lang.String alias)
public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String fullQuery()
public TopiaQuery<E> addParam(java.lang.String id,
java.lang.Object e)
id - identification of the param in the querye - value of the param
protected java.util.List<java.lang.Object> getParams()
public TopiaQuery<E> addLoad(java.lang.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 java.util.List<java.lang.String> getPropertiesToLoad()
public TopiaQuery<E> add(java.lang.String where)
where - element to add
public TopiaQuery<E> add(java.lang.String paramName,
TopiaQuery.Op constraint,
java.lang.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 java.lang.String getValueName(java.lang.String paramName)
public TopiaQuery<E> addNotNull(java.lang.String paramName)
paramName - name of the parameter in the query
public TopiaQuery<E> add(java.lang.String paramName,
java.lang.Object paramValue)
paramName - name of the parameter in the queryparamValue - value of the parameter
#add(java.lang.String, fr.ifremer.suiviobsmer.TopiaQuery.Op, java.lang.Object)
public TopiaQuery<E> add(java.lang.String paramName,
java.util.Collection<java.lang.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(java.lang.String paramName,
java.util.Collection<java.lang.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(java.util.Map<java.lang.String,java.lang.Object> properties)
properties -
public TopiaQuery<E> addFrom(java.lang.String str)
str - the element to add
public TopiaQuery<E> setSelect(java.lang.String select)
select - element to add
public TopiaQuery<E> addDistinct()
public TopiaQuery<E> addOrder(java.lang.String order)
order - element to add
public TopiaQuery<E> addOrderDesc(java.lang.String order)
public TopiaQuery<E> addGroup(java.lang.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
public TopiaQuery<E> setMaxResults(int max)
max - the number of elements wanted
public java.util.List execute(TopiaContext transaction)
throws TopiaException
transaction - the TopiaContext to use for execution
TopiaExceptionTopiaContext.find(java.lang.String, java.lang.Object[])
public java.util.List execute()
throws TopiaException
TopiaExceptionexecute(org.nuiton.topia.TopiaContext)
public java.util.List<E> executeToEntityList(TopiaContext transaction)
throws TopiaException,
java.lang.ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
java.lang.ClassCastException
public java.util.List<E> executeToEntityList()
throws TopiaException,
java.lang.ClassCastException
TopiaException
java.lang.ClassCastExceptionexecuteToEntityList(org.nuiton.topia.TopiaContext)
public <K> java.util.Map<K,E> executeToEntityMap(TopiaContext transaction,
java.lang.String keyName,
java.lang.Class<K> keyClass)
throws TopiaException,
java.lang.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
java.lang.ClassCastException
public <K> java.util.Map<K,E> executeToEntityMap(java.lang.String keyName,
java.lang.Class<K> keyClass)
throws TopiaException,
java.lang.ClassCastException
K - keyName - keyClass -
TopiaException
java.lang.ClassCastExceptionexecuteToEntityMap(org.nuiton.topia.TopiaContext, java.lang.String, java.lang.Class)
public java.util.Map<java.lang.String,E> executeToEntityMap(TopiaContext transaction)
throws TopiaException,
java.lang.ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
java.lang.ClassCastException
public java.util.Map<java.lang.String,E> executeToEntityMap()
throws TopiaException,
java.lang.ClassCastException
TopiaException
java.lang.ClassCastExceptionexecuteToEntityMap(org.nuiton.topia.TopiaContext)
public E executeToEntity(TopiaContext transaction)
throws TopiaException,
java.lang.ClassCastException
addLoad(java.lang.String[]).
transaction - the TopiaContext to use for execution
TopiaException
java.lang.ClassCastException
public E executeToEntity()
throws TopiaException,
java.lang.ClassCastException
TopiaException
java.lang.ClassCastExceptionexecuteToEntity(org.nuiton.topia.TopiaContext)
public int executeToInteger(TopiaContext transaction,
java.lang.String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : SUM(myParam))
TopiaException
public int executeToInteger(java.lang.String select)
throws TopiaException
select -
TopiaExceptionexecuteToInteger(org.nuiton.topia.TopiaContext, java.lang.String)
public java.lang.String executeToString(TopiaContext transaction,
java.lang.String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : MAX(myParam))
TopiaException
public java.lang.String executeToString(java.lang.String select)
throws TopiaException
select -
TopiaExceptionexecuteToString(org.nuiton.topia.TopiaContext, java.lang.String)
public java.lang.Object executeToObject(TopiaContext transaction,
java.lang.String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden
TopiaException
public java.lang.Object executeToObject(java.lang.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,
java.lang.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> java.lang.Object loadProperty(T entity,
java.lang.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 | |||||||||