Package de.hybris.platform.core
Class GenericQuery
- java.lang.Object
-
- de.hybris.platform.core.FlexibleSearchTranslatable
-
- de.hybris.platform.core.GenericQuery
-
- All Implemented Interfaces:
java.io.Serializable
public class GenericQuery extends FlexibleSearchTranslatable
TheGenericQuery
class holds and manages all objects necessary for searching on a specificType
. That include Conditions, SelectFields and OrderBy as well as Joins and SubQueries.To setup a particular query, you have to do the following:
// assuming we've got a type to search upon GenericQuery query = new GenericQuery(MyModel._TYPECODE); query.addCondition( GenericCondition.createConditionForValueComparison(new GenericSearchField(MyModel.ATTRIBUTE), Operator.EQUALS, "test")); query.addOrderBy(new GenericSearchOrderBy(new GenericSearchField(MyModel.ATTRIBUTE), true)); final SearchResult<MyModel> searchResult = genericSearchService.search(gsquery); final List<MyModel> result = searchResult.getResult();
Also quite common is joining types:
// create query as usual GenericQuery query = new GenericQuery( MyModel._TYPECODE ); // now add joined type and specify join condition query.addInnerJoin( MyOtherTypeModel._TYPECODE , GenericCondition.createConditionForFieldComparison( new GenericSearchField(MyModel._TYPECODE, MyModel.ATTRIBUTE), Operator.EQUALS, new GenericSearchField(MyOtherTypeModel._TYPECODE, MyOtherTypeModel.ATTRIBUTE), ) ); // now we may add query conditions for both types query.addCondition( GenericCondition.createIsNotNullCondition( new GenericSearchField( MyModel._TYPECODE, MyModel.ATTRIBUTE ) ) ); query.addCondition( GenericCondition.createIsNullCondition( new GenericSearchField(MyOtherType._TYPECODE, MyOtherTypeModel.ATTRIBUTE ) ) ); // ... execute search as usual using
GenericSearchService
And instead of searching for Items, you're able to define your own select fields. Please note that the correct return class for each select field must be provided.
... query.addSelectField( new GenericSelectField( MyModel._TYPECODE, MyModel.ATTRIBUTE, String.class ) ); ...
If the same type occurs several time within one query it is necessary to use aliases for each type.
// the initial type does not need to have a alias GenericQuery query = new GenericQuery( MyModel._TYPECODE ); // now join the same type again query.addInnerJoin( MyModel._TYPECODE , "alias1", GenericCondition.getComparison( new GenericSearchField( T.ATTR1 ), // fields of the initial type dont need aliases either Operator.EQUALS, new GenericSearchField( "alias1", T.ATTR2 ) // this field refers to the joined type! ) ); // and again ... query.addInnerJoin( MyModel._TYPECODE, "alias2", GenericCondition.getComparison( new GenericSearchField( "alias1" T.ATTR3 ), Operator.EQUALS, new GenericSearchField( "alias2", T.ATTR4 ) ) );
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GenericQuery(java.lang.String typeCode)
GenericQuery(java.lang.String typeCode, boolean typeExclusive)
GenericQuery(java.lang.String typeCode, GenericCondition condition)
GenericQuery(java.lang.String typeCode, GenericCondition condition, boolean typeExclusive)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GenericQuery
addCondition(GenericCondition condition)
Adds aGenericCondition
to this query.
GenericQuery
addConditions(GenericCondition... conditions)
Adds one or more conditions to this query at once.GenericTypeJoin
addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition)
GenericTypeJoin
addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
GenericTypeJoin
addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
Adds an InnerJoin to the query.GenericTypeJoin
addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
Adds an InnerJoin to the query.GenericQuery
addOrderBy(GenericSearchOrderBy orderBy)
Adds anGenericSearchOrderBy
to the query.GenericTypeJoin
addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition)
GenericTypeJoin
addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
GenericTypeJoin
addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
Adds an OuterJoin to the query.GenericTypeJoin
addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
Adds an OuterJoin to the query.GenericQuery
addSelectField(GenericSelectField field)
Adds aGenericSelectField
to the query.GenericQuery
addSubQuery(GenericSearchField field, Operator operator, java.lang.String subqueryTypeCode)
Creates an adds a newGenericSubQueryCondition
to this query.GenericQuery
addSubQuery(Operator operator, java.lang.String subqueryTypeCode)
GenericQuery
addSubQuery(java.lang.String fieldQualifier, Operator operator, java.lang.String subqueryTypeCode)
GenericCondition
getCondition()
Returns the condition hold by this instance.java.lang.String
getInitialTypeCode()
Returns the initial type code.java.util.Collection<GenericSearchOrderBy>
getOrderByList()
Returns a copy of the orderBy list.java.util.List<java.lang.Class>
getResultClasses()
Returns a List containing all resultClass
es which have been previously set.java.util.List<GenericSelectField>
getSelectFields()
Returns a List containingGenericSelectField
s instances which have been previously set.java.util.Collection<GenericTypeJoin>
getTypeJoinList()
Returns a copy of the join list, containingGenericTypeJoin
instances.boolean
isTranslatableToPolyglotDialect()
boolean
isTypeExclusive()
Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is falseprotected void
setCondition(GenericCondition genericCondition)
void
setInitialTypeAlias(java.lang.String alias)
protected void
setInitialTypeCode(java.lang.String typeCode)
Sets the initial type code.void
setTypeExclusive(boolean exclusive)
Marks the initial type exclusive, which means subtype are excluded from searching; default is false.void
toFlexibleSearch(java.lang.StringBuilder queryBuffer, java.util.Map<java.lang.String,java.lang.String> aliasTypeMap, java.util.Map<java.lang.String,java.lang.Object> valueMap)
compiles this instance in order to append its query snippet and add its value(s), if anyjava.lang.String
toFlexibleSearch(java.util.Map valueMap)
Convenience method instead of usingtoFlexibleSearch(StringBuilder, Map, Map)
.void
toPolyglotSearch(java.lang.StringBuilder queryBuffer, java.util.Map<java.lang.String,java.lang.String> aliasTypeMap, java.util.Map<java.lang.String,java.lang.Object> valueMap)
compiles this instance in order to append its query snippet and add its value(s), if anyjava.lang.String
toPolyglotSearch(java.util.Map valueMap)
-
Methods inherited from class de.hybris.platform.core.FlexibleSearchTranslatable
getAliasFromTypeMap
-
-
-
-
Constructor Detail
-
GenericQuery
public GenericQuery(java.lang.String typeCode, GenericCondition condition, boolean typeExclusive)
-
GenericQuery
public GenericQuery(java.lang.String typeCode, GenericCondition condition)
-
GenericQuery
public GenericQuery(java.lang.String typeCode)
-
GenericQuery
public GenericQuery(java.lang.String typeCode, boolean typeExclusive)
-
-
Method Detail
-
getInitialTypeCode
public java.lang.String getInitialTypeCode()
Returns the initial type code.- Returns:
- the initial type code
-
setInitialTypeCode
protected void setInitialTypeCode(java.lang.String typeCode)
Sets the initial type code.- Parameters:
typeCode
- the new initial type code
-
setInitialTypeAlias
public void setInitialTypeAlias(java.lang.String alias)
-
isTypeExclusive
public boolean isTypeExclusive()
Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is false- Returns:
true
if the initial type is marked exclusive
-
setTypeExclusive
public void setTypeExclusive(boolean exclusive)
Marks the initial type exclusive, which means subtype are excluded from searching; default is false.- Parameters:
exclusive
-true
marks the initial type as exclusive type
-
addCondition
public GenericQuery addCondition(GenericCondition condition)
Adds aGenericCondition
to this query. A
GenericConditionList
is created automatically if necessary.
- Parameters:
condition
- theGenericCondition
which will be added
-
addConditions
public GenericQuery addConditions(GenericCondition... conditions)
Adds one or more conditions to this query at once. If the query will contain more than one condition afterwards it automatically wraps them into aGenericConditionList
.- Parameters:
conditions
- the new conditions
-
getCondition
public GenericCondition getCondition()
Returns the condition hold by this instance.- Returns:
- the condition
-
setCondition
protected void setCondition(GenericCondition genericCondition)
- Parameters:
genericCondition
- The condition to be set.
-
getOrderByList
public java.util.Collection<GenericSearchOrderBy> getOrderByList()
Returns a copy of the orderBy list.- Returns:
- orderBy list
-
addOrderBy
public GenericQuery addOrderBy(GenericSearchOrderBy orderBy)
Adds anGenericSearchOrderBy
to the query.- Parameters:
orderBy
- theGenericSearchOrderBy
instance to add
-
addOuterJoin
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
Adds an OuterJoin to the query.- Parameters:
joinCondition
- theGenericCondition
instance, representing the join
-
addOuterJoin
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition)
-
addOuterJoin
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
Adds an OuterJoin to the query.- Parameters:
joinCondition
- theGenericCondition
instance, representing the joinisTypeExclusive
- whether the joined type is marked exclusive
-
addOuterJoin
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
-
addInnerJoin
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
Adds an InnerJoin to the query.- Parameters:
joinCondition
- theGenericCondition
instance, representing the join
-
addInnerJoin
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition)
-
addInnerJoin
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
Adds an InnerJoin to the query.- Parameters:
joinCondition
- theGenericCondition
instance, representing the joinisTypeExclusive
- whether the joined type is marked exclusive
-
addInnerJoin
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
-
getTypeJoinList
public java.util.Collection<GenericTypeJoin> getTypeJoinList()
Returns a copy of the join list, containingGenericTypeJoin
instances.- Returns:
- join list
-
addSubQuery
public GenericQuery addSubQuery(GenericSearchField field, Operator operator, java.lang.String subqueryTypeCode)
Creates an adds a newGenericSubQueryCondition
to this query.- Parameters:
field
- the field to compare the subquery result tooperator
- the comparison operatorsubqueryTypeCode
- the inital type code of the subquery- Returns:
- the subquery object
-
addSubQuery
public GenericQuery addSubQuery(java.lang.String fieldQualifier, Operator operator, java.lang.String subqueryTypeCode)
-
addSubQuery
public GenericQuery addSubQuery(Operator operator, java.lang.String subqueryTypeCode)
-
addSelectField
public GenericQuery addSelectField(GenericSelectField field)
Adds aGenericSelectField
to the query.- Parameters:
field
- theGenericSearchField
which will be added
-
getResultClasses
public java.util.List<java.lang.Class> getResultClasses()
Returns a List containing all resultClass
es which have been previously set.- Returns:
- list of classes
-
getSelectFields
public java.util.List<GenericSelectField> getSelectFields()
Returns a List containingGenericSelectField
s instances which have been previously set.- Returns:
- list of SelectFields
-
toFlexibleSearch
public java.lang.String toFlexibleSearch(java.util.Map valueMap)
Convenience method instead of usingtoFlexibleSearch(StringBuilder, Map, Map)
.- Parameters:
valueMap
- the value map to be passed to flexiblesearch (which is filled automatically)- Returns:
- the generated flexiblesearch query
-
toPolyglotSearch
public java.lang.String toPolyglotSearch(java.util.Map valueMap)
-
toFlexibleSearch
public void toFlexibleSearch(java.lang.StringBuilder queryBuffer, java.util.Map<java.lang.String,java.lang.String> aliasTypeMap, java.util.Map<java.lang.String,java.lang.Object> valueMap)
Description copied from class:FlexibleSearchTranslatable
compiles this instance in order to append its query snippet and add its value(s), if any- Specified by:
toFlexibleSearch
in classFlexibleSearchTranslatable
- Parameters:
queryBuffer
- contains the queryaliasTypeMap
- contains typeCode <> typeIndex mappingsvalueMap
- contains valueQualifier <> value mappings- See Also:
FlexibleSearchTranslatable.toFlexibleSearch(java.lang.StringBuilder, java.util.Map, java.util.Map)
-
toPolyglotSearch
public void toPolyglotSearch(java.lang.StringBuilder queryBuffer, java.util.Map<java.lang.String,java.lang.String> aliasTypeMap, java.util.Map<java.lang.String,java.lang.Object> valueMap)
Description copied from class:FlexibleSearchTranslatable
compiles this instance in order to append its query snippet and add its value(s), if any- Overrides:
toPolyglotSearch
in classFlexibleSearchTranslatable
- Parameters:
queryBuffer
- contains the queryaliasTypeMap
- contains typeCode <> typeIndex mappingsvalueMap
- contains valueQualifier <> value mappings
-
isTranslatableToPolyglotDialect
public boolean isTranslatableToPolyglotDialect()
-
-