Purpose: This policy defines the configuration options for a Query By Example query.
Description:
A Query By Example query is an ObjectLevelReadQuery where the
selection criteria is built from an example domain object passed in via setExampleObject.
If no policy is specified the selection criteria is built from the example object in the following way:
null are ignored.
0 for int) are ignored.
A policy can be set on the query to:
null
or the default value for its type. See alwaysIncludeAttribute.
excludeValue.
null attribute on the example object with domain objects that have
either null for that attribute also, or have set a meaningful (notNull) value
for that attribute. See setShouldUseEqualityForNulls.
addSpecialOperation.
Note
When setting an attribute on the example object which is itself a java object with an ObjectReferenceMapping, the mapped components of that attribute will be considered, not the entire object. There is no limit to how many mapped objects can be nested inside the example object.
Note
setExampleObject is different from setSelectionObject in
ReadObjectQuery which reads a single object by first extracting
the primary key from the selection object.
Restrictions:
setValidateExample
should be set to true on the corresponding QueryByExamplePolicy to ensure no unsupported relationship types are used in the example.Example:
// This example uses like for Strings and the salary must be greater
// than zero.
ReadAllQuery query = new ReadAllQuery();
Employee employee = new Employee();
employee.setFirstName("B%");
employee.setLastName("S%");
employee.setSalary(0);
query.setExampleObject(employee);
QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "greaterThan");
policy.alwaysIncludeAttribute(Employee.class, "salary");
query.setQueryByExamplePolicy(policy);
Vector results = (Vector) session.executeQuery(query);
ObjectLevelReadQuery#setExampleObject, ObjectLevelReadQuery#setQueryByExamplePolicy
| Modifier and Type | Field and Description |
|---|---|
| public Map | |
| public boolean | |
| public Map | |
| protected boolean | |
| public Map |
| Access | Constructor and Description |
|---|---|
| public | QueryByExamplePolicy()
PUBLIC: Constructs a default policy equal to that used when no policy is specified. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addSpecialOperation(Class<?>
Attribute values of which type, for instance
attributeValueClass, String Integer, to apply to. Note for int attributes the
class is Integer.class not int.class. This is not
the Class of the example object the attribute is an instance variable of.Name of method in operation)Expression usedPUBLIC:
Allows operations other than |
| public void | alwaysIncludeAttribute(Class<?>
The class that the attribute belongs to, normally this is the example class unless using nested QBE. exampleClass, String The name of a mapped attribute. attributeName)PUBLIC: Always considers the value for a particular attribute as meaningful in a query by example. |
| public Expression | completeExpression(Expression expression, Object attributeValue, Class<?> attributeValueClass)
INTERNAL: This method is used to determine which operation to use for comparison (equal, or a special operation). |
| public Expression | completeExpressionForNull(Expression expression)
INTERNAL: This method is used when the attribute value is null, but it has to be included at all times. |
| public void | excludeDefaultPrimitiveValues()
PUBLIC: Ignores attributes set to the default value for their primitive type. |
| public void | excludeValue(byte value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(char value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(double value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(float value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(int value)
PUBLIC: An attribute in the example object set to be an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(long value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(Object value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(short value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public void | excludeValue(boolean value)
PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example. |
| public Map | getAttributesToAlwaysInclude()
PUBLIC:
Attributes to always consider even if set to |
| public String | |
| public Map | Returns: A hashtable where the keys areClass objects and the values
are the names of operations to use for attributes of that Class.PUBLIC:
The special operations to use in place of |
| public Map | Returns: valuesToExclude The keys and values are values to exclude (key == value). Primitives are wrapped, soint 0 will be stored as Integer(0).PUBLIC: Decides which attributes to ignore based on the values they are set to. |
| public void | includeAllValues()
PUBLIC: Considers all mapped attributes in the example object as meaningful in a Query By Example. |
| public boolean | isAlwaysIncluded(Class<?> theClass, String attributeName)
INTERNAL: returns whether the attributeName is to be always included. |
| public boolean | isExcludedValue(Object value)
INTERNAL: returns if the value is in the values to be excluded automatically. |
| public void | removeFromValuesToExclude(Object
No attributes set to value)value will be excluded from a Query By Example.
value.getClass() is a key of the Hashtable returned by getValuesToExclude.Note There is a distinction between an attribute and the value
it is set to. An attribute can be included independently of its value with
Note
PUBLIC: Considers all attributes set to a previously excluded value on the example object. |
| public void | setAttributesToAlwaysInclude(Map<K, V> newAttributesToAlwaysInclude)
INTERNAL: It is possible to generate a Hashtable (keys are the Class, and values the attribute names) of the attributes to be included at all times (even if the value is null, or the value belongs to the values to be excluced automatically). |
| public void | setShouldUseEqualityForNulls(boolean
If true (by default) uses shouldUseEqualityForNulls)isNull else notNull.PUBLIC:
Matches an included |
| public void | setSpecialOperations(Map<K, V>
A hashtable where the keys are newOperations)Class objects and the values
are the names of operations to use for attributes of that Class.PUBLIC:
The special operations to use in place of |
| public void | setValidateExample(boolean validate)
PUBLIC:
When set to |
| public void | setValuesToExclude(Map<K, V>
The keys and values are values to exclude (key == value). Primitives are
wrapped, so newValuesToExclude)int 0 will be stored as Integer(0).PUBLIC: Decides which attributes to ignore based on the values they are set to. |
| public boolean | shouldIncludeInQuery(Class<?> aClass, String attributeName, Object attributeValue)
INTERNAL: This method determines whether an attribute pair is be included in the query. |
| public boolean | Returns: If true (by default) usesisNull else notNull.PUBLIC:
Matches an included |
| public boolean | shouldValidateExample()
PUBLIC: Returns true if the example object used with this policy should be validated for attributes with unsupported mappings. |
| attributesToAlwaysInclude | back to summary |
|---|---|
| public Map<K, V> attributesToAlwaysInclude | |
| shouldUseEqualityForNulls | back to summary |
|---|---|
| public boolean shouldUseEqualityForNulls | |
| specialOperations | back to summary |
|---|---|
| public Map<K, V> specialOperations | |
| validateExample | back to summary |
|---|---|
| protected boolean validateExample | |
| valuesToExclude | back to summary |
|---|---|
| public Map<K, V> valuesToExclude | |
| QueryByExamplePolicy | back to summary |
|---|---|
| public QueryByExamplePolicy() PUBLIC: Constructs a default policy equal to that used when no policy is specified. Sets the default values to be excluded, (that includes 0, false, empty String, etc).
By default if an attribute is | |
| addSpecialOperation | back to summary |
|---|---|
| public void addSpecialOperation(Class<?> attributeValueClass, String operation) PUBLIC:
Allows operations other than
For example if an attribute of type
Any comparison operation in Note A special operation can not be used for attributes set to | |
| alwaysIncludeAttribute | back to summary |
|---|---|
| public void alwaysIncludeAttribute(Class<?> exampleClass, String attributeName) PUBLIC: Always considers the value for a particular attribute as meaningful in a query by example.
Required to override the normal behavior which is to ignore an
attribute of the example object if set to
Example: To find all projects without a budget set | |
| completeExpression | back to summary |
|---|---|
| public Expression completeExpression(Expression expression, Object attributeValue, Class<?> attributeValueClass) INTERNAL: This method is used to determine which operation to use for comparison (equal, or a special operation). | |
| completeExpressionForNull | back to summary |
|---|---|
| public Expression completeExpressionForNull(Expression expression) INTERNAL: This method is used when the attribute value is null, but it has to be included at all times. It determines whether to use isNull, or notNull. | |
| excludeDefaultPrimitiveValues | back to summary |
|---|---|
| public void excludeDefaultPrimitiveValues() PUBLIC: Ignores attributes set to the default value for their primitive type.
For instance Called by the constructor. | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(byte value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(char value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(double value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(float value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(int value) PUBLIC: An attribute in the example object set to be an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(long value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(Object value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for Note
| |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(short value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| excludeValue | back to summary |
|---|---|
| public void excludeValue(boolean value) PUBLIC: An attribute in the example object set to an excluded value will be ignored in a Query By Example.
The default excluded value for | |
| getAttributesToAlwaysInclude | back to summary |
|---|---|
| public Map PUBLIC:
Attributes to always consider even if set to
| |
| getOperation | back to summary |
|---|---|
| public String getOperation(Class<?> aClass) INTERNAL: determines which operation to use for comparison. | |
| getSpecialOperations | back to summary |
|---|---|
| public Map PUBLIC:
The special operations to use in place of
| |
| getValuesToExclude | back to summary |
|---|---|
| public Map PUBLIC: Decides which attributes to ignore based on the values they are set to. If an attribute of the example domain object is set to one of these values it will be ignored, and not considered in the query.
Attributes set to excluded values are not always ignored.
See
| |
| includeAllValues | back to summary |
|---|---|
| public void includeAllValues() PUBLIC: Considers all mapped attributes in the example object as meaningful in a Query By Example. Note Even attributes of the example object that are not set, and therefore zero or empty by default, will be included.
Reverses a previous call to | |
| isAlwaysIncluded | back to summary |
|---|---|
| public boolean isAlwaysIncluded(Class<?> theClass, String attributeName) INTERNAL: returns whether the attributeName is to be always included. | |
| isExcludedValue | back to summary |
|---|---|
| public boolean isExcludedValue(Object value) INTERNAL: returns if the value is in the values to be excluded automatically. | |
| removeFromValuesToExclude | back to summary |
|---|---|
| public void removeFromValuesToExclude(Object value) PUBLIC: Considers all attributes set to a previously excluded value on the example object. Primitive values to be removed must first be wrapped inside an Object.
| |
| setAttributesToAlwaysInclude | back to summary |
|---|---|
| public void setAttributesToAlwaysInclude(Map<K, V> newAttributesToAlwaysInclude) INTERNAL: It is possible to generate a Hashtable (keys are the Class, and values the attribute names) of the attributes to be included at all times (even if the value is null, or the value belongs to the values to be excluced automatically). | |
| setShouldUseEqualityForNulls | back to summary |
|---|---|
| public void setShouldUseEqualityForNulls(boolean shouldUseEqualityForNulls) PUBLIC:
Matches an included
Set to
Example: to find all Employees with an assigned Note Unless an attribute set to
| |
| setSpecialOperations | back to summary |
|---|---|
| public void setSpecialOperations(Map<K, V> newOperations) PUBLIC:
The special operations to use in place of
| |
| setValidateExample | back to summary |
|---|---|
| public void setValidateExample(boolean validate) PUBLIC:
When set to | |
| setValuesToExclude | back to summary |
|---|---|
| public void setValuesToExclude(Map<K, V> newValuesToExclude) PUBLIC: Decides which attributes to ignore based on the values they are set to. An attribute of the example domain object set to one of these values will be ignored, and not considered in the query.
Attributes set to excluded values are not always ignored.
See
| |
| shouldIncludeInQuery | back to summary |
|---|---|
| public boolean shouldIncludeInQuery(Class<?> aClass, String attributeName, Object attributeValue) INTERNAL: This method determines whether an attribute pair is be included in the query. | |
| shouldUseEqualityForNulls | back to summary |
|---|---|
| public boolean shouldUseEqualityForNulls() PUBLIC:
Matches an included
Set to
Example: to find all Employees with an assigned
| |
| shouldValidateExample | back to summary |
|---|---|
| public boolean shouldValidateExample() PUBLIC: Returns true if the example object used with this policy should be validated for attributes with unsupported mappings. | |