org.apache.ojb.broker.query
Class Criteria

java.lang.Object
  extended by org.apache.ojb.broker.query.Criteria
All Implemented Interfaces:
java.io.Serializable

public class Criteria
extends java.lang.Object
implements java.io.Serializable

Persistent Criteria can be used to retrieve sets of objects based on their attributes Normally each attribute is ANDed together, an OR can be performed by creating a new PersistentCriteria and adding it.

Criteria are used, rather than a simple string, because they can be precompiled for efficiency. This code is based on stuff from COBRA - Java Object Persistence Layer Copyright (C) 1997, 1998 DB Harvey-George eMail: cobra@lowrent.org

Version:
$Id: Criteria.java 365232 2005-12-21 22:36:07Z tomdz $
Author:
Jakob Braeuchi
See Also:
Serialized Form

Field Summary
static int AND
          criteria is AND-ed with it's parent
static int NONE
          criteria has no parent
static int OR
          criteria is OR-ed with it's parent
static java.lang.String PARENT_QUERY_PREFIX
          prefix to identify attributes referencing enclosing query
 
Constructor Summary
Criteria()
          Constructor declaration
Criteria(SelectionCriteria aSelectionCriteria)
          Constructor with a SelectionCriteria
 
Method Summary
 void addAndCriteria(Criteria pc)
          ANDs two sets of criteria together:
 void addBetween(java.lang.Object attribute, java.lang.Object value1, java.lang.Object value2)
          Adds BETWEEN criteria, customer_id between 1 and 10
 void addColumnEqualTo(java.lang.String column, java.lang.Object value)
          Adds and equals (=) criteria, CUST_ID = 10034 attribute will NOT be translated into column name
 void addColumnEqualToField(java.lang.String column, java.lang.Object fieldName)
          Adds and equals (=) criteria for field comparison.
 void addColumnIn(java.lang.String column, java.util.Collection values)
          Adds IN criteria, customer_id in(1,10,33,44) large values are split into multiple InCriteria IN (1,10) OR IN(33, 44)
The attribute will NOT be translated into column name
 void addColumnIsNull(java.lang.String column)
          Adds is Null criteria, customer_id is Null The attribute will NOT be translated into column name
 void addColumnNotNull(java.lang.String column)
          Adds not Null criteria, customer_id is not Null The attribute will NOT be translated into column name
 void addEqualTo(java.lang.String attribute, java.lang.Object value)
          Adds and equals (=) criteria, customer_id = 10034
 void addEqualToColumn(java.lang.String attribute, java.lang.String colName)
          Adds and equals (=) criteria for column comparison.
 void addEqualToField(java.lang.String attribute, java.lang.String fieldName)
          Adds and equals (=) criteria for field comparison.
 void addExists(Query subQuery)
          Adds an exists(sub query)
 void addGreaterOrEqualThan(java.lang.Object attribute, java.lang.Object value)
          Adds GreaterOrEqual Than (>=) criteria, customer_id >= 10034
 void addGreaterOrEqualThanField(java.lang.String attribute, java.lang.Object value)
          Adds GreaterOrEqual Than (>=) criteria, customer_id >= person_id
 void addGreaterThan(java.lang.Object attribute, java.lang.Object value)
          Adds Greater Than (>) criteria, customer_id > 10034
 void addGreaterThanField(java.lang.String attribute, java.lang.Object value)
          Adds Greater Than (>) criteria, customer_id > person_id
 void addGroupBy(FieldHelper aField)
          Deprecated. use QueryByCriteria#addGroupBy
 void addGroupBy(java.lang.String fieldName)
          Deprecated. use QueryByCriteria#addGroupBy
 void addGroupBy(java.lang.String[] fieldNames)
          Deprecated. use QueryByCriteria#addGroupBy
 void addIn(java.lang.Object attribute, Query subQuery)
          IN Criteria with SubQuery
 void addIn(java.lang.String attribute, java.util.Collection values)
          Adds IN criteria, customer_id in(1,10,33,44) large values are split into multiple InCriteria IN (1,10) OR IN(33, 44)
 void addIsNull(java.lang.String attribute)
          Adds is Null criteria, customer_id is Null
 void addLessOrEqualThan(java.lang.Object attribute, java.lang.Object value)
          Adds LessOrEqual Than (<=) criteria, customer_id <= 10034
 void addLessOrEqualThanField(java.lang.String attribute, java.lang.Object value)
          Adds LessOrEqual Than (<=) criteria, customer_id <= person_id
 void addLessThan(java.lang.Object attribute, java.lang.Object value)
          Adds Less Than (<) criteria, customer_id < 10034
 void addLessThanField(java.lang.String attribute, java.lang.Object value)
          Adds Less Than (<) criteria, customer_id < person_id
 void addLike(java.lang.Object attribute, java.lang.Object value)
          Adds Like (LIKE) criteria, customer_name LIKE "m%ller"
 void addNotBetween(java.lang.Object attribute, java.lang.Object value1, java.lang.Object value2)
          Adds NOT BETWEEN criteria, customer_id not between 1 and 10
 void addNotEqualTo(java.lang.Object attribute, java.lang.Object value)
          Adds NotEqualTo (<>) criteria, customer_id <> 10034
 void addNotEqualToColumn(java.lang.String attribute, java.lang.String colName)
          Adds and equals (<>) criteria for column comparison.
 void addNotEqualToField(java.lang.String attribute, java.lang.String fieldName)
          Adds and equals (=) criteria for field comparison.
 void addNotExists(Query subQuery)
          Adds a not exists(sub query)
 void addNotIn(java.lang.String attribute, java.util.Collection values)
          Adds NOT IN criteria, customer_id not in(1,10,33,44) large values are split into multiple InCriteria NOT IN (1,10) AND NOT IN(33, 44)
 void addNotIn(java.lang.String attribute, Query subQuery)
          NOT IN Criteria with SubQuery
 void addNotLike(java.lang.String attribute, java.lang.Object value)
          Adds Like (NOT LIKE) criteria, customer_id NOT LIKE 10034
 void addNotNull(java.lang.String attribute)
          Adds not Null criteria, customer_id is not Null
 void addOrCriteria(Criteria pc)
          ORs two sets of criteria together: active = true AND balance < 0 OR active = true AND overdraft = 0
 void addOrderBy(FieldHelper aField)
          Deprecated. use QueryByCriteria#addOrderBy
 void addOrderBy(java.lang.String fieldName)
          Deprecated. use #addOrderByAscending(String fieldName)
 void addOrderBy(java.lang.String fieldName, boolean sortAscending)
          Deprecated. use QueryByCriteria#addOrderBy
 void addOrderByAscending(java.lang.String fieldName)
          Deprecated. use QueryByCriteria#addOrderByAscending
 void addOrderByDescending(java.lang.String fieldName)
          Deprecated. use QueryByCriteria#addOrderByDescending
 void addPathClass(java.lang.String aPath, java.lang.Class aClass)
          Add a hint Class for a path.
 void addPrefetchedRelationship(java.lang.String aName)
          Deprecated. use QueryByCriteria#addPrefetchedRelationship
 void addSql(java.lang.String anSqlStatment)
          Adds freeform SQL criteria, REVERSE(name) like 're%'
 Criteria copy(boolean includeGroupBy, boolean includeOrderBy, boolean includePrefetchedRelationships)
          make a copy of the criteria
 java.lang.String getAlias()
           
 java.util.List getClassesForPath(java.lang.String aPath)
          Get the a List of Class objects used as hints for a path
 java.util.Enumeration getElements()
          Get an Enumeration with all sub criteria
 Criteria getParentCriteria()
           
 java.util.Map getPathClasses()
          Gets the pathClasses.
 QueryByCriteria getQuery()
           
 int getType()
          Answer the type
 UserAlias getUserAlias()
           
 boolean isEmbraced()
          ANDed criteria are embraced
 boolean isEmpty()
          Answer true if no sub criteria available
 boolean isNegative()
           
 void setAlias(java.lang.String alias)
          Sets the alias.
 void setAlias(java.lang.String alias, java.lang.String aliasPath)
          Sets the alias.
 void setAlias(UserAlias userAlias)
          Sets the alias using a userAlias object.
 void setEmbraced(boolean embraced)
          Set embraced
 void setNegative(boolean negative)
          Flags the whole Criteria as negative.
 void setPathClass(java.lang.String aPath, java.lang.Class aClass)
          Set the Class for a path.
 void setType(int type)
          Set the type
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OR

public static final int OR
criteria is OR-ed with it's parent

See Also:
Constant Field Values

AND

public static final int AND
criteria is AND-ed with it's parent

See Also:
Constant Field Values

NONE

public static final int NONE
criteria has no parent

See Also:
Constant Field Values

PARENT_QUERY_PREFIX

public static final java.lang.String PARENT_QUERY_PREFIX
prefix to identify attributes referencing enclosing query

See Also:
Constant Field Values
Constructor Detail

Criteria

public Criteria()
Constructor declaration


Criteria

public Criteria(SelectionCriteria aSelectionCriteria)
Constructor with a SelectionCriteria

Parameters:
aSelectionCriteria - SelectionCriteria
Method Detail

copy

public Criteria copy(boolean includeGroupBy,
                     boolean includeOrderBy,
                     boolean includePrefetchedRelationships)
make a copy of the criteria

Parameters:
includeGroupBy - if true
includeOrderBy - if ture
includePrefetchedRelationships - if true
Returns:
a copy of the criteria

getElements

public java.util.Enumeration getElements()
Get an Enumeration with all sub criteria

Returns:
Enumeration

getType

public int getType()
Answer the type

Returns:
int

setType

public void setType(int type)
Set the type

Parameters:
type - OR, AND, NONE

isEmbraced

public boolean isEmbraced()
ANDed criteria are embraced

Returns:
true if embraced,

setEmbraced

public void setEmbraced(boolean embraced)
Set embraced

Parameters:
embraced - true if criteria is to be surrounded by braces

addEqualTo

public void addEqualTo(java.lang.String attribute,
                       java.lang.Object value)
Adds and equals (=) criteria, customer_id = 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addColumnEqualTo

public void addColumnEqualTo(java.lang.String column,
                             java.lang.Object value)
Adds and equals (=) criteria, CUST_ID = 10034 attribute will NOT be translated into column name

Parameters:
column - The column name to be used without translation
value - An object representing the value of the column

addColumnEqualToField

public void addColumnEqualToField(java.lang.String column,
                                  java.lang.Object fieldName)
Adds and equals (=) criteria for field comparison. The field name will be translated into the appropriate columnName by SqlStatement. The attribute will NOT be translated into column name

Parameters:
column - The column name to be used without translation
fieldName - An object representing the value of the field

addEqualToField

public void addEqualToField(java.lang.String attribute,
                            java.lang.String fieldName)
Adds and equals (=) criteria for field comparison. The field name will be translated into the appropriate columnName by SqlStatement.
name = boss.name

Parameters:
attribute - The field name to be used
fieldName - The field name to compare with

addNotEqualToField

public void addNotEqualToField(java.lang.String attribute,
                               java.lang.String fieldName)
Adds and equals (=) criteria for field comparison. The field name will be translated into the appropriate columnName by SqlStatement.
name <> boss.name

Parameters:
attribute - The field name to be used
fieldName - The field name to compare with

addNotEqualToColumn

public void addNotEqualToColumn(java.lang.String attribute,
                                java.lang.String colName)
Adds and equals (<>) criteria for column comparison. The column Name will NOT be translated.
name <> T_BOSS.LASTNMAE

Parameters:
attribute - The field name to be used
colName - The name of the column to compare with

addEqualToColumn

public void addEqualToColumn(java.lang.String attribute,
                             java.lang.String colName)
Adds and equals (=) criteria for column comparison. The column Name will NOT be translated.
name = T_BOSS.LASTNMAE

Parameters:
attribute - The field name to be used
colName - The name of the column to compare with

addGreaterOrEqualThan

public void addGreaterOrEqualThan(java.lang.Object attribute,
                                  java.lang.Object value)
Adds GreaterOrEqual Than (>=) criteria, customer_id >= 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addGreaterOrEqualThanField

public void addGreaterOrEqualThanField(java.lang.String attribute,
                                       java.lang.Object value)
Adds GreaterOrEqual Than (>=) criteria, customer_id >= person_id

Parameters:
attribute - The field name to be used
value - The field name to compare with

addLessOrEqualThan

public void addLessOrEqualThan(java.lang.Object attribute,
                               java.lang.Object value)
Adds LessOrEqual Than (<=) criteria, customer_id <= 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addLessOrEqualThanField

public void addLessOrEqualThanField(java.lang.String attribute,
                                    java.lang.Object value)
Adds LessOrEqual Than (<=) criteria, customer_id <= person_id

Parameters:
attribute - The field name to be used
value - The field name to compare with

addLike

public void addLike(java.lang.Object attribute,
                    java.lang.Object value)
Adds Like (LIKE) criteria, customer_name LIKE "m%ller"

Parameters:
attribute - The field name to be used
value - An object representing the value of the field
See Also:
LikeCriteria

addNotLike

public void addNotLike(java.lang.String attribute,
                       java.lang.Object value)
Adds Like (NOT LIKE) criteria, customer_id NOT LIKE 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field
See Also:
LikeCriteria

addNotEqualTo

public void addNotEqualTo(java.lang.Object attribute,
                          java.lang.Object value)
Adds NotEqualTo (<>) criteria, customer_id <> 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addGreaterThan

public void addGreaterThan(java.lang.Object attribute,
                           java.lang.Object value)
Adds Greater Than (>) criteria, customer_id > 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addGreaterThanField

public void addGreaterThanField(java.lang.String attribute,
                                java.lang.Object value)
Adds Greater Than (>) criteria, customer_id > person_id

Parameters:
attribute - The field name to be used
value - The field to compare with

addLessThan

public void addLessThan(java.lang.Object attribute,
                        java.lang.Object value)
Adds Less Than (<) criteria, customer_id < 10034

Parameters:
attribute - The field name to be used
value - An object representing the value of the field

addLessThanField

public void addLessThanField(java.lang.String attribute,
                             java.lang.Object value)
Adds Less Than (<) criteria, customer_id < person_id

Parameters:
attribute - The field name to be used
value - The field to compare with

addOrderBy

public void addOrderBy(java.lang.String fieldName)
Deprecated. use #addOrderByAscending(String fieldName)

Adds a field for orderBy, order is ASCENDING

Parameters:
fieldName - The field name to be used

addOrderBy

public void addOrderBy(java.lang.String fieldName,
                       boolean sortAscending)
Deprecated. use QueryByCriteria#addOrderBy

Adds a field for orderBy

Parameters:
fieldName - the field name to be used
sortAscending - true for ASCENDING, false for DESCENDING

addOrderBy

public void addOrderBy(FieldHelper aField)
Deprecated. use QueryByCriteria#addOrderBy

Adds a field for orderBy

Parameters:
aField - the Field

addOrderByAscending

public void addOrderByAscending(java.lang.String fieldName)
Deprecated. use QueryByCriteria#addOrderByAscending

Adds a field for orderBy ASCENDING

Parameters:
fieldName - The field name to be used

addOrderByDescending

public void addOrderByDescending(java.lang.String fieldName)
Deprecated. use QueryByCriteria#addOrderByDescending

Adds a field for orderBy DESCENDING

Parameters:
fieldName - The field name to be used

addOrCriteria

public void addOrCriteria(Criteria pc)
ORs two sets of criteria together:
 active = true AND balance < 0 OR active = true AND overdraft = 0
 

Parameters:
pc - criteria

addIsNull

public void addIsNull(java.lang.String attribute)
Adds is Null criteria, customer_id is Null

Parameters:
attribute - The field name to be used

addColumnIsNull

public void addColumnIsNull(java.lang.String column)
Adds is Null criteria, customer_id is Null The attribute will NOT be translated into column name

Parameters:
column - The column name to be used without translation

addNotNull

public void addNotNull(java.lang.String attribute)
Adds not Null criteria, customer_id is not Null

Parameters:
attribute - The field name to be used

addColumnNotNull

public void addColumnNotNull(java.lang.String column)
Adds not Null criteria, customer_id is not Null The attribute will NOT be translated into column name

Parameters:
column - The column name to be used without translation

addBetween

public void addBetween(java.lang.Object attribute,
                       java.lang.Object value1,
                       java.lang.Object value2)
Adds BETWEEN criteria, customer_id between 1 and 10

Parameters:
attribute - The field name to be used
value1 - The lower boundary
value2 - The upper boundary

addNotBetween

public void addNotBetween(java.lang.Object attribute,
                          java.lang.Object value1,
                          java.lang.Object value2)
Adds NOT BETWEEN criteria, customer_id not between 1 and 10

Parameters:
attribute - The field name to be used
value1 - The lower boundary
value2 - The upper boundary

addIn

public void addIn(java.lang.String attribute,
                  java.util.Collection values)
Adds IN criteria, customer_id in(1,10,33,44) large values are split into multiple InCriteria IN (1,10) OR IN(33, 44)

Parameters:
attribute - The field name to be used
values - The value Collection

addColumnIn

public void addColumnIn(java.lang.String column,
                        java.util.Collection values)
Adds IN criteria, customer_id in(1,10,33,44) large values are split into multiple InCriteria IN (1,10) OR IN(33, 44)
The attribute will NOT be translated into column name

Parameters:
column - The column name to be used without translation
values - The value Collection

addNotIn

public void addNotIn(java.lang.String attribute,
                     java.util.Collection values)
Adds NOT IN criteria, customer_id not in(1,10,33,44) large values are split into multiple InCriteria NOT IN (1,10) AND NOT IN(33, 44)

Parameters:
attribute - The field name to be used
values - The value Collection

addIn

public void addIn(java.lang.Object attribute,
                  Query subQuery)
IN Criteria with SubQuery

Parameters:
attribute - The field name to be used
subQuery - The subQuery

addNotIn

public void addNotIn(java.lang.String attribute,
                     Query subQuery)
NOT IN Criteria with SubQuery

Parameters:
attribute - The field name to be used
subQuery - The subQuery

addSql

public void addSql(java.lang.String anSqlStatment)
Adds freeform SQL criteria, REVERSE(name) like 're%'

Parameters:
anSqlStatment - The free form SQL-Statement

addAndCriteria

public void addAndCriteria(Criteria pc)
ANDs two sets of criteria together:

Parameters:
pc - criteria

addExists

public void addExists(Query subQuery)
Adds an exists(sub query)

Parameters:
subQuery - sub-query

addNotExists

public void addNotExists(Query subQuery)
Adds a not exists(sub query)

Parameters:
subQuery - sub-query

isEmpty

public boolean isEmpty()
Answer true if no sub criteria available

Returns:
boolean

addGroupBy

public void addGroupBy(java.lang.String fieldName)
Deprecated. use QueryByCriteria#addGroupBy

Adds a groupby fieldName for ReportQueries.

Parameters:
fieldName - The groupby to set

addGroupBy

public void addGroupBy(FieldHelper aField)
Deprecated. use QueryByCriteria#addGroupBy

Adds a field for groupby

Parameters:
aField - the Field

addGroupBy

public void addGroupBy(java.lang.String[] fieldNames)
Deprecated. use QueryByCriteria#addGroupBy

Adds an array of groupby fieldNames for ReportQueries.

Parameters:
fieldNames - The groupby to set

addPrefetchedRelationship

public void addPrefetchedRelationship(java.lang.String aName)
Deprecated. use QueryByCriteria#addPrefetchedRelationship

add the name of a Relationship for prefetch read

Parameters:
aName - the name of the relationship

getAlias

public java.lang.String getAlias()
Returns:
String

getUserAlias

public UserAlias getUserAlias()
Returns:
String

setAlias

public void setAlias(java.lang.String alias)
Sets the alias. Empty String is regarded as null.

Parameters:
alias - The alias to set

setAlias

public void setAlias(java.lang.String alias,
                     java.lang.String aliasPath)
Sets the alias. Empty String is regarded as null.

Parameters:
alias - The alias to set
aliasPath - The path segment(s) to which the alias applies

setAlias

public void setAlias(UserAlias userAlias)
Sets the alias using a userAlias object.

Parameters:
userAlias - The alias to set

getQuery

public QueryByCriteria getQuery()
Returns:
the query containing the criteria

getParentCriteria

public Criteria getParentCriteria()
Returns:
the parent criteria

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

isNegative

public boolean isNegative()
Returns:
Returns the negative.

setNegative

public void setNegative(boolean negative)
Flags the whole Criteria as negative.

Parameters:
negative - The negative to set.

addPathClass

public void addPathClass(java.lang.String aPath,
                         java.lang.Class aClass)
Add a hint Class for a path. Used for relationships to extents.
SqlStatment will use these hint classes when resolving the path. Without these hints SqlStatment will use the base class the relationship points to ie: Article instead of CdArticle.

Parameters:
aPath - the path segment ie: allArticlesInGroup
aClass - the Class ie: CdArticle
See Also:
org.apache.ojb.broker.QueryTest#testInversePathExpression()

setPathClass

public void setPathClass(java.lang.String aPath,
                         java.lang.Class aClass)
Set the Class for a path. Used for relationships to extents.
SqlStatment will use this class when resolving the path. Without this hint SqlStatment will use the base class the relationship points to ie: Article instead of CdArticle. Using this method is the same as adding just one hint

Parameters:
aPath - the path segment ie: allArticlesInGroup
aClass - the Class ie: CdArticle
See Also:
org.apache.ojb.broker.QueryTest#testInversePathExpression(), addPathClass(java.lang.String, java.lang.Class)

getClassesForPath

public java.util.List getClassesForPath(java.lang.String aPath)
Get the a List of Class objects used as hints for a path

Parameters:
aPath - the path segment ie: allArticlesInGroup
Returns:
a List o Class objects to be used in SqlStatment
See Also:
addPathClass(java.lang.String, java.lang.Class), org.apache.ojb.broker.QueryTest#testInversePathExpression()

getPathClasses

public java.util.Map getPathClasses()
Gets the pathClasses. A Map containing hints about what Class to be used for what path segment If local instance not set, try parent Criteria's instance. If this is the top-level Criteria, try the m_query's instance

Returns:
Returns a Map


(C) 2002 - 2006 Apache Software Foundation
All rights reserved. Published under the Apache License 2.0.
http://db.apache.org/ojb
Version: 1.0.4, 2005-12-30