|
||||||||||
| 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...).
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 boolean |
distinct
|
protected Integer |
endIndex
|
protected StringBuilder |
from
From part of the query * |
static String |
FROM_SEPARATOR_DEFAULT
|
static String |
FROM_SEPARATOR_JOIN
|
static String |
FROM_SEPARATOR_LEFT_JOIN
|
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. |
|
| Method Summary | ||
|---|---|---|
TopiaQuery |
add(Map<String,Object> properties)
Deprecated. since 2.3.4 use addEquals(Map) |
|
TopiaQuery |
add(String where)
Deprecated. since 2.3.4, use addWhere(String) instead |
|
TopiaQuery |
add(String paramName,
Object... paramValue)
Deprecated. since 2.3.4, use addEquals(String, Object...) instead |
|
TopiaQuery |
add(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
Deprecated. since 2.3.4, use addWhere(String, Op, Object) instead |
|
TopiaQuery |
addBetween(String paramName,
Object value1,
Object value2)
Add an element with BETWEEN operation. |
|
TopiaQuery |
addDistinct()
Add the distinct key word in the query. |
|
TopiaQuery |
addEquals(Map<String,Object> properties)
Add a map of properties to the where clause of the query. |
|
TopiaQuery |
addEquals(String paramName,
Object... paramValue)
Add an element to the query. |
|
TopiaQuery |
addFetch(String... properties)
Used to load properties during query execution using FETCH keyword. |
|
TopiaQuery |
addFilter(EntityFilter filter)
Add a filter to the query that contains limit indexes,
orderBy condition and referenceId if needed. |
|
TopiaQuery |
addFilter(EntityFilter filter,
String propertyToFilter)
Add a filter to the query that contains limit indexes,
orderBy condition and referenceId if needed. |
|
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)
Deprecated. since 2.3.4 use correct addFrom or addJoin or addLeftJoin |
|
protected TopiaQuery |
addFrom(String separator,
String property,
String alias)
Add an element to the from in the query. |
|
protected TopiaQuery |
addFromJoin(String separator,
String property,
String alias,
boolean fetch)
|
|
TopiaQuery |
addGroup(String... group)
Add an element to the group of the query. |
|
TopiaQuery |
addInElements(String elementProperty,
String containerProperty)
Add link constraint between two properties. |
|
TopiaQuery |
addJoin(String property,
String alias,
boolean fetch)
Add a inner join property to the query with alias. |
|
TopiaQuery |
addLeftJoin(String property,
String alias,
boolean fetch)
Add a left join property to the query with alias. |
|
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 |
addNull(String paramName)
Add an element to the query with the constraint 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. |
|
TopiaQuery |
addSubQuery(String queryPart,
TopiaQuery subquery)
Method used to add a subquery in an existing query. |
|
TopiaQuery |
addWhere(String where)
Add a where element to the Query. |
|
TopiaQuery |
addWhere(String paramName,
TopiaQuery.Op operator,
Object paramValue)
Add an element to the query. |
|
protected String |
convertStringArray(String... array)
Helper method for array type. |
|
List |
execute(TopiaContext transaction)
Simple execution of the query. |
|
int |
executeCount(TopiaContext transaction)
Execute a simple count on the query, i.e. the number of results get from the query. |
|
|
executeToEntity(TopiaContext transaction,
Class<E> entityClass)
Execute the query and get the first result entity. |
|
|
executeToEntityList(TopiaContext transaction,
Class<E> entityClass)
Execute the query and get a List of entity. |
|
|
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(TopiaContext transaction,
String select)
Execute the query and get an Integer for result. |
|
Object |
executeToObject(TopiaContext transaction,
String select)
Execute the query and get an Object for result. |
|
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. |
|
String |
getPropertyCreateDate(String alias)
|
|
String |
getPropertyId(String alias)
|
|
String |
getPropertyVersion(String alias)
|
|
protected String |
getValueName(String paramName)
|
|
protected
|
loadEntityProperty(T entity,
String property)
Load a property of type TopiaEntity from an other entity. |
|
protected void |
loadProperties(TopiaEntity 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()
|
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final String FROM_SEPARATOR_DEFAULT
public static final String FROM_SEPARATOR_JOIN
public static final String FROM_SEPARATOR_LEFT_JOIN
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
| Constructor Detail |
|---|
public TopiaQuery()
public TopiaQuery(Class<? extends TopiaEntity> mainEntityClass)
entityClass. The from statement
is automatically set.
mainEntityClass - used as from part of the query
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 - used as from part of the queryalias - for the mainEntityClass| 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
@Deprecated public TopiaQuery addFrom(String str)
str - the element to add
addFrom(Class, String),
addJoin(String, String, boolean),
addLeftJoin(String, String, boolean)
protected TopiaQuery addFrom(String separator,
String property,
String alias)
separator.
property - the property to addseparator - The separator to use before adding the element (if null
the FROM_SEPARATOR_DEFAULT will be used).
public TopiaQuery addJoin(String property,
String alias,
boolean fetch)
property to the query with alias.
The join is done in From statement as : FROM Contact C JOIN C.boat B. The
added part is 'JOIN C.boat B' using addJoin("C.boat", "B", false). The order
of calling addFrom(Class, String) or this method is very important.
The first element in the FROM is always the main entity of the query.
property - Property name to use as a Joinalias - Alias of the property in the queryfetch - Add FETCH keyword to load the property in result (avoid
lazy initialization)
public TopiaQuery addLeftJoin(String property,
String alias,
boolean fetch)
property to the query with alias.
The join is done in From statement as : FROM Contact C LEFT JOIN C.boat B.
The added part is 'LEFT JOIN C.boat B' using addJoin("C.boat", "B", false).
The order of calling addFrom(Class, String) or this method is
very important. The first element in the FROM is always the main entity
of the query.
property - Property name to use as a Joinalias - Alias of the property in the queryfetch - Add FETCH keyword to load the property in result (avoid
lazy initialization)
protected TopiaQuery addFromJoin(String separator,
String property,
String alias,
boolean fetch)
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass)
entityClass - different from the mainEntity
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass,
String alias)
entityClass - different from the mainEntityalias - of the entity in the query
public 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 - List of properties to load
public TopiaQuery addFetch(String... properties)
addLoad(String...) or load manually the entities
wanted.
properties - Properties to load during query execution
protected List<String> getPropertiesToLoad()
@Deprecated public TopiaQuery add(String where)
addWhere(String) instead
where - Where statement to add
public TopiaQuery addWhere(String where)
where - element to add
@Deprecated
public TopiaQuery add(String paramName,
TopiaQuery.Op constraint,
Object paramValue)
addWhere(String, Op, Object) instead
paramName - name of the parameter to addconstraint - constraint to useparamValue - value of this parameter
public TopiaQuery addWhere(String paramName,
TopiaQuery.Op operator,
Object paramValue)
operator is needed to determine what type of operation it is.
Ex : add("boat", Op.EQ, boat) means -> boat = :boat. Also if the paramValue
is Null, the paramName will be added to the query with the constraint
null (IS NULL).
TODO-fdesbois-2010-05-26 : maybe manage more than one paramValue with Object... Depends on operator
paramName - the name of the parameter in the query (attribute of
the entity)operator - the operation concernedparamValue - the value of the parameter (an other entity, a String,
...)
protected String getValueName(String paramName)
public TopiaQuery add(String paramName,
Object... paramValue)
addEquals(String, Object...) instead
paramName - name of the parameter to addparamValue - value of this parameter
public TopiaQuery addEquals(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.
You can also set paramValue to null if you want the paramName to be null in the query.
paramName - name of the parameter in the queryparamValue - values of the parameter
addWhere(String, Op, Object)@Deprecated public TopiaQuery add(Map<String,Object> properties)
addEquals(Map)
properties - map of the properties to add
public TopiaQuery addEquals(Map<String,Object> properties)
properties - to add to the query
public TopiaQuery addNotNull(String paramName)
paramName - name of the parameter in the query
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,
...)
public TopiaQuery addNull(String paramName)
paramName - name of the parameter in the query
public TopiaQuery addBetween(String paramName,
Object value1,
Object value2)
paramName will be
found between value1 and value2. Useful for date
manipulations.
paramName - The name of the parameter in the query (entity property)value1 - First valuevalue2 - Second value
public TopiaQuery addInElements(String elementProperty,
String containerProperty)
elementProperty is in
elements of containerProperty which is a collection with same type
than elementProperty. (HQL : elementProperty IN elements
(containerProperty))
elementProperty - contains in containerProperty collectioncontainerProperty - collection which contains elementProperty
public TopiaQuery addSubQuery(String queryPart,
TopiaQuery subquery)
subquery in WHERE part of the
query. The queryPart is the element in the query to bind with the
subquery. The ? character is used to inject the subquery into the
queryPart. Ex :
// Add a SUB_ELMT = (subquery) into the query
query.addSubQuery("SUB_ELMT = (?)", subquery, false);
queryPart - part of the query where subquery need to be injectedsubquery - existing topiaQuery as subquery
getValueName(String)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 TopiaQuery addFilter(EntityFilter filter)
throws IllegalArgumentException
filter to the query that contains limit indexes,
orderBy condition and referenceId if needed. The referenceProperty is
necessary to use the referenceId of the filter. The filter will
be applied on the main entity in the query (using the mainAlias if
necessary).
Note : the default orderBy is the topiaCreateDate ordered desc (the most
recent in first)
filter - Filter to apply on the query
IllegalArgumentException - if referenceId is defined but no
referenceProperty was setaddFilter(EntityFilter, String)
public TopiaQuery addFilter(EntityFilter filter,
String propertyToFilter)
throws IllegalArgumentException
filter to the query that contains limit indexes,
orderBy condition and referenceId if needed. In some case it's necessary
to specify explicitely the propertyToFilter in complex queries.
The referenceProperty need to be specifie in filter to have a
correspondance between the referenceId and it's property in the query. By
default, the propertyToFilter is the mainAlias of the query.
Note : the default orderBy is the topiaCreateDate ordered desc (the most
recent in first)
filter - Filter to apply on the querypropertyToFilter - Explicit property to filter
IllegalArgumentException - if referenceId is defined but no
referenceProperty was set
public List execute(TopiaContext transaction)
throws TopiaException
transaction - the TopiaContext to use for execution
TopiaException - for error on query executionTopiaContext.find(String, Object...)
public <E extends TopiaEntity> List<E> executeToEntityList(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - entity typetransaction - the TopiaContext to use for executionentityClass - used to check return type of execution results
TopiaException - for error on query execution
ClassCastException - if entityClass doesn't match to results
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 - entity typeK - the type of the map keytransaction - the TopiaContext to use for executionentityClass - needed to execute the querykeyName - the property name of the key in the entitykeyClass - the key class for the result map
TopiaException - for error on query execution
ClassCastException - if entityClass doesn't match to results
public <E extends TopiaEntity> Map<String,E> executeToEntityMap(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - entity typetransaction - the TopiaContext to use for executionentityClass - used to check return type of execution results
TopiaException - for error on query execution
ClassCastException - if entityClass doesn't match to results
public <E extends TopiaEntity> E executeToEntity(TopiaContext transaction,
Class<E> entityClass)
throws TopiaException,
ClassCastException
addLoad(String...).
E - entity typetransaction - the TopiaContext to use for executionentityClass - used to check return type of execution results
TopiaException - for error on query execution
ClassCastException - if entityClass doesn't match to results
public Object executeToObject(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden
TopiaException - for error on query execution
public int executeToInteger(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : SUM(myParam))
TopiaException - for error on query execution
public String executeToString(TopiaContext transaction,
String select)
throws TopiaException
transaction - the TopiaContext to use for executionselect - the Select overriden (ex : MAX(myParam))
TopiaException - for error on query execution
public int executeCount(TopiaContext transaction)
throws TopiaException
COUNT(DISTINCT mainAlias)
transaction - the TopiaContext to use for execution
TopiaException - for error on query execution
protected void loadProperties(TopiaEntity entity)
throws TopiaException
entity - used to load properties
TopiaException - for error on query execution
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 - for error on query execution
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 - for error loading property (encapsulate
IllegalACessException, InvocationTargetException,
NoSuchMethodException)public static String getProperty(String... entityProperty)
getProperty("boat", Boat.SHIP_OWNER, ShipOwner.NAME);
// will return boat.shipOwner.name
It's better to use constants instead of directly the string chain to
avoid problems on changing property name in model. Furthermore it's
better to use this method instead of doing :
"boat." + Boat.SHIP_OWNER + "." + ShipOwner.NAME
entityProperty - to concat
public String getPropertyId(String alias)
public String getPropertyCreateDate(String alias)
public String getPropertyVersion(String alias)
protected void finalize()
throws Throwable
finalize in class ObjectThrowablepublic String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||