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
TheGenericQueryclass 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 usingGenericSearchServiceAnd 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 GenericQueryaddCondition(GenericCondition condition)Adds aGenericConditionto this query.GenericQueryaddConditions(GenericCondition... conditions)Adds one or more conditions to this query at once.GenericTypeJoinaddInnerJoin(java.lang.String typeCode, GenericCondition joinCondition)GenericTypeJoinaddInnerJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)GenericTypeJoinaddInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)Adds an InnerJoin to the query.GenericTypeJoinaddInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)Adds an InnerJoin to the query.GenericQueryaddOrderBy(GenericSearchOrderBy orderBy)Adds anGenericSearchOrderByto the query.GenericTypeJoinaddOuterJoin(java.lang.String typeCode, GenericCondition joinCondition)GenericTypeJoinaddOuterJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)GenericTypeJoinaddOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)Adds an OuterJoin to the query.GenericTypeJoinaddOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)Adds an OuterJoin to the query.GenericQueryaddSelectField(GenericSelectField field)Adds aGenericSelectFieldto the query.GenericQueryaddSubQuery(GenericSearchField field, Operator operator, java.lang.String subqueryTypeCode)Creates an adds a newGenericSubQueryConditionto this query.GenericQueryaddSubQuery(Operator operator, java.lang.String subqueryTypeCode)GenericQueryaddSubQuery(java.lang.String fieldQualifier, Operator operator, java.lang.String subqueryTypeCode)GenericConditiongetCondition()Returns the condition hold by this instance.java.lang.StringgetInitialTypeCode()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 resultClasses which have been previously set.java.util.List<GenericSelectField>getSelectFields()Returns a List containingGenericSelectFields instances which have been previously set.java.util.Collection<GenericTypeJoin>getTypeJoinList()Returns a copy of the join list, containingGenericTypeJoininstances.booleanisTranslatableToPolyglotDialect()booleanisTypeExclusive()Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is falseprotected voidsetCondition(GenericCondition genericCondition)voidsetInitialTypeAlias(java.lang.String alias)protected voidsetInitialTypeCode(java.lang.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(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.StringtoFlexibleSearch(java.util.Map valueMap)Convenience method instead of usingtoFlexibleSearch(StringBuilder, Map, Map).voidtoPolyglotSearch(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.StringtoPolyglotSearch(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:
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
public GenericQuery addCondition(GenericCondition condition)
Adds aGenericConditionto this query. AGenericConditionListis created automatically if necessary.- Parameters:
condition- theGenericConditionwhich 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 anGenericSearchOrderByto the query.- Parameters:
orderBy- theGenericSearchOrderByinstance to add
-
addOuterJoin
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
Adds an OuterJoin to the query.- Parameters:
joinCondition- theGenericConditioninstance, 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- theGenericConditioninstance, 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- theGenericConditioninstance, 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- theGenericConditioninstance, 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, containingGenericTypeJoininstances.- Returns:
- join list
-
addSubQuery
public GenericQuery addSubQuery(GenericSearchField field, Operator operator, java.lang.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
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 aGenericSelectFieldto the query.- Parameters:
field- theGenericSearchFieldwhich will be added
-
getResultClasses
public java.util.List<java.lang.Class> getResultClasses()
Returns a List containing all resultClasses which have been previously set.- Returns:
- list of classes
-
getSelectFields
public java.util.List<GenericSelectField> getSelectFields()
Returns a List containingGenericSelectFields 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: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:
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: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()
-
-