Package de.hybris.platform.core
Class GenericQuery
java.lang.Object
de.hybris.platform.core.FlexibleSearchTranslatable
de.hybris.platform.core.GenericQuery
- All Implemented Interfaces:
Serializable
The
GenericQuery class holds and manages all objects necessary for searching on a specific
Type. 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:
-
Constructor Summary
ConstructorsConstructorDescriptionGenericQuery(String typeCode) GenericQuery(String typeCode, boolean typeExclusive) GenericQuery(String typeCode, GenericCondition condition) GenericQuery(String typeCode, GenericCondition condition, boolean typeExclusive) -
Method Summary
Modifier and TypeMethodDescriptionaddCondition(GenericCondition condition) Adds aGenericConditionto this query.addConditions(GenericCondition... conditions) Adds one or more conditions to this query at once.addInnerJoin(String typeCode, GenericCondition joinCondition) addInnerJoin(String typeCode, GenericCondition joinCondition, boolean isTypeExclusive) addInnerJoin(String typeCode, String alias, GenericCondition joinCondition) Adds an InnerJoin to the query.addInnerJoin(String typeCode, String alias, GenericCondition joinCondition, boolean isTypeExclusive) Adds an InnerJoin to the query.addOrderBy(GenericSearchOrderBy orderBy) Adds anGenericSearchOrderByto the query.addOuterJoin(String typeCode, GenericCondition joinCondition) addOuterJoin(String typeCode, GenericCondition joinCondition, boolean isTypeExclusive) addOuterJoin(String typeCode, String alias, GenericCondition joinCondition) Adds an OuterJoin to the query.addOuterJoin(String typeCode, String alias, GenericCondition joinCondition, boolean isTypeExclusive) Adds an OuterJoin to the query.addSelectField(GenericSelectField field) Adds aGenericSelectFieldto the query.addSubQuery(GenericSearchField field, Operator operator, String subqueryTypeCode) Creates an adds a newGenericSubQueryConditionto this query.addSubQuery(Operator operator, String subqueryTypeCode) addSubQuery(String fieldQualifier, Operator operator, String subqueryTypeCode) Returns the condition hold by this instance.Returns the initial type code.Returns a copy of the orderBy list.Returns a List containing all resultClasses which have been previously set.Returns a List containingGenericSelectFields instances which have been previously set.Returns a copy of the join list, containingGenericTypeJoininstances.booleanbooleanIndicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is falseprotected voidsetCondition(GenericCondition genericCondition) voidsetInitialTypeAlias(String alias) protected voidsetInitialTypeCode(String typeCode) Sets the initial type code.voidsetTypeExclusive(boolean exclusive) Marks the initial type exclusive, which means subtype are excluded from searching; default is false.voidtoFlexibleSearch(StringBuilder queryBuffer, Map<String, String> aliasTypeMap, Map<String, Object> valueMap) compiles this instance in order to append its query snippet and add its value(s), if anytoFlexibleSearch(Map valueMap) Convenience method instead of usingtoFlexibleSearch(StringBuilder, Map, Map).voidtoPolyglotSearch(StringBuilder queryBuffer, Map<String, String> aliasTypeMap, Map<String, Object> valueMap) compiles this instance in order to append its query snippet and add its value(s), if anytoPolyglotSearch(Map valueMap) Methods inherited from class de.hybris.platform.core.FlexibleSearchTranslatable
getAliasFromTypeMap
-
Constructor Details
-
GenericQuery
-
GenericQuery
-
GenericQuery
-
GenericQuery
-
-
Method Details
-
getInitialTypeCode
Returns the initial type code.- Returns:
- the initial type code
-
setInitialTypeCode
Sets the initial type code.- Parameters:
typeCode- the new initial type code
-
setInitialTypeAlias
-
isTypeExclusive
public boolean isTypeExclusive()Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is false- Returns:
trueif 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-truemarks the initial type as exclusive type
-
addCondition
Adds aGenericConditionto this query. AGenericConditionListis created automatically if necessary.- Parameters:
condition- theGenericConditionwhich will be added
-
addConditions
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
Returns the condition hold by this instance.- Returns:
- the condition
-
setCondition
- Parameters:
genericCondition- The condition to be set.
-
getOrderByList
Returns a copy of the orderBy list.- Returns:
- orderBy list
-
addOrderBy
Adds anGenericSearchOrderByto the query.- Parameters:
orderBy- theGenericSearchOrderByinstance to add
-
addOuterJoin
Adds an OuterJoin to the query.- Parameters:
joinCondition- theGenericConditioninstance, representing the join
-
addOuterJoin
-
addOuterJoin
public GenericTypeJoin addOuterJoin(String typeCode, String alias, GenericCondition joinCondition, boolean isTypeExclusive) Adds an OuterJoin to the query.- Parameters:
joinCondition- theGenericConditioninstance, representing the joinisTypeExclusive- whether the joined type is marked exclusive
-
addOuterJoin
public GenericTypeJoin addOuterJoin(String typeCode, GenericCondition joinCondition, boolean isTypeExclusive) -
addInnerJoin
Adds an InnerJoin to the query.- Parameters:
joinCondition- theGenericConditioninstance, representing the join
-
addInnerJoin
-
addInnerJoin
public GenericTypeJoin addInnerJoin(String typeCode, String alias, GenericCondition joinCondition, boolean isTypeExclusive) Adds an InnerJoin to the query.- Parameters:
joinCondition- theGenericConditioninstance, representing the joinisTypeExclusive- whether the joined type is marked exclusive
-
addInnerJoin
public GenericTypeJoin addInnerJoin(String typeCode, GenericCondition joinCondition, boolean isTypeExclusive) -
getTypeJoinList
Returns a copy of the join list, containingGenericTypeJoininstances.- Returns:
- join list
-
addSubQuery
public GenericQuery addSubQuery(GenericSearchField field, Operator operator, String subqueryTypeCode) Creates an adds a newGenericSubQueryConditionto 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
-
addSubQuery
-
addSelectField
Adds aGenericSelectFieldto the query.- Parameters:
field- theGenericSearchFieldwhich will be added
-
getResultClasses
Returns a List containing all resultClasses which have been previously set.- Returns:
- list of classes
-
getSelectFields
Returns a List containingGenericSelectFields instances which have been previously set.- Returns:
- list of SelectFields
-
toFlexibleSearch
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
-
toFlexibleSearch
public void toFlexibleSearch(StringBuilder queryBuffer, Map<String, String> aliasTypeMap, Map<String, Object> valueMap) Description copied from class:FlexibleSearchTranslatablecompiles this instance in order to append its query snippet and add its value(s), if any- Specified by:
toFlexibleSearchin classFlexibleSearchTranslatable- Parameters:
queryBuffer- contains the queryaliasTypeMap- contains typeCode <> typeIndex mappingsvalueMap- contains valueQualifier <> value mappings- See Also:
-
toPolyglotSearch
public void toPolyglotSearch(StringBuilder queryBuffer, Map<String, String> aliasTypeMap, Map<String, Object> valueMap) Description copied from class:FlexibleSearchTranslatablecompiles this instance in order to append its query snippet and add its value(s), if any- Overrides:
toPolyglotSearchin classFlexibleSearchTranslatable- Parameters:
queryBuffer- contains the queryaliasTypeMap- contains typeCode <> typeIndex mappingsvalueMap- contains valueQualifier <> value mappings
-
isTranslatableToPolyglotDialect
public boolean isTranslatableToPolyglotDialect()
-