Top Description Inners Fields Constructors Methods
org.hibernate.criterion

public Class Example

extends Object
implements Criterion
Class Inheritance
All Implemented Interfaces
org.hibernate.criterion.Criterion, java.io.Serializable
Imports
java.io.Serializable, java.util.ArrayList, .HashSet, .List, .Locale, .Set, org.hibernate.Criteria, .EntityMode, org.hibernate.engine.spi.TypedValue, org.hibernate.internal.util.StringHelper, org.hibernate.persister.entity.EntityPersister, org.hibernate.type.CompositeType, .Type

Support for query by example.
List results = session.createCriteria(Parent.class)
    .add( Example.create(parent).ignoreCase() )
    .createCriteria("child")
        .add( Example.create( parent.getChild() ) )
    .list();
"Examples" may be mixed and matched with "Expressions" in the same Criteria.
Author
Gavin King
See Also
org.hibernate.Criteria

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Example.AllPropertySelector

Property selector that includes all properties

public static class
Example.NotNullOrZeroPropertySelector

Property selector that includes only properties that are not null and non-zero (if numeric)

public static class
Example.NotNullPropertySelector

Property selector that includes only properties that are not null

public static interface
Example.PropertySelector

A strategy for choosing property values for inclusion in the query criteria.

Field Summary

Modifier and TypeField and Description
private Character
private final Object
private final Set<String>
private boolean
private boolean
private MatchMode
private Example.PropertySelector

Constructor Summary

AccessConstructor and Description
protected
Example(Object
The example bean
exampleEntity
,
Example.PropertySelector
The property selector to use
selector
)

Allow subclasses to instantiate as needed.

Method Summary

Modifier and TypeMethod and Description
protected void
addComponentTypedValues(String path, Object component, CompositeType type, List<TypedValue> list, Criteria criteria, CriteriaQuery criteriaQuery)

protected void
protected void
appendComponentCondition(String path, Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, StringBuilder buf)

protected void
appendPropertyCondition(String propertyName, Object propertyValue, Criteria criteria, CriteriaQuery cq, StringBuilder buf)

public static Example

Returns:

a new instance of Example
create
(Object
The example bean to use.
exampleEntity
)

Create a new Example criterion instance, which includes all non-null properties by default

public Example

Returns:

this, for method chaining
enableLike
()

Use the "like" operator for all string-valued properties.

public Example

Returns:

this, for method chaining
enableLike
(MatchMode
The match mode to use.
matchMode
)

Use the "like" operator for all string-valued properties

public Example

Returns:

this, for method chaining
excludeNone
()

Include all properties.

public Example

Returns:

this, for method chaining
excludeProperty
(String
The name of the property to exclude
name
)

Exclude a particular property by name.

public Example

Returns:

this, for method chaining
excludeZeroes
()

Exclude zero-valued properties.

private EntityMode
getEntityMode(Criteria criteria, CriteriaQuery criteriaQuery)

public TypedValue[]
getTypedValues(Criteria
The local criteria
criteria
,
CriteriaQuery
The overall criteria query
criteriaQuery
)

Implements org.hibernate.criterion.Criterion.getTypedValues.

Return typed values for all parameters in the rendered SQL fragment
public Example

Returns:

this, for method chaining
ignoreCase
()

Ignore case for all string-valued properties

private boolean
isPropertyIncluded(Object value, String name, Type type)

public Example

Returns:

this, for method chaining
setEscapeCharacter
(Character
The escape character
escapeCharacter
)

Set escape character for "like" clause if like matching was enabled

public Example

Returns:

this, for method chaining
setPropertySelector
(Example.PropertySelector
The selector to use
selector
)

Set the property selector to use.

public String
toSqlString(Criteria
The local criteria
criteria
,
CriteriaQuery
The overall criteria query
criteriaQuery
)

Implements org.hibernate.criterion.Criterion.toSqlString.

Render the SQL fragment
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

escapeCharacterback to summary
private Character escapeCharacter
exampleEntityback to summary
private final Object exampleEntity
excludedPropertiesback to summary
private final Set<String> excludedProperties
isIgnoreCaseEnabledback to summary
private boolean isIgnoreCaseEnabled
isLikeEnabledback to summary
private boolean isLikeEnabled
matchModeback to summary
private MatchMode matchMode
selectorback to summary
private Example.PropertySelector selector

Constructor Detail

Exampleback to summary
protected Example(Object exampleEntity, Example.PropertySelector selector)

Allow subclasses to instantiate as needed.

Parameters
exampleEntity:Object

The example bean

selector:Example.PropertySelector

The property selector to use

Method Detail

addComponentTypedValuesback to summary
protected void addComponentTypedValues(String path, Object component, CompositeType type, List<TypedValue> list, Criteria criteria, CriteriaQuery criteriaQuery)
addPropertyTypedValueback to summary
protected void addPropertyTypedValue(Object value, Type type, List<TypedValue> list)
appendComponentConditionback to summary
protected void appendComponentCondition(String path, Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, StringBuilder buf)
appendPropertyConditionback to summary
protected void appendPropertyCondition(String propertyName, Object propertyValue, Criteria criteria, CriteriaQuery cq, StringBuilder buf)
createback to summary
public static Example create(Object exampleEntity)

Create a new Example criterion instance, which includes all non-null properties by default

Parameters
exampleEntity:Object

The example bean to use.

Returns:Example

a new instance of Example

enableLikeback to summary
public Example enableLike()

Use the "like" operator for all string-valued properties. This form implicitly uses MatchMode#EXACT

Returns:Example

this, for method chaining

enableLikeback to summary
public Example enableLike(MatchMode matchMode)

Use the "like" operator for all string-valued properties

Parameters
matchMode:MatchMode

The match mode to use.

Returns:Example

this, for method chaining

excludeNoneback to summary
public Example excludeNone()

Include all properties. Equivalent to calling setPropertySelector passing in AllPropertySelector#INSTANCE

Returns:Example

this, for method chaining

See Also
setPropertySelector
excludePropertyback to summary
public Example excludeProperty(String name)

Exclude a particular property by name.

Parameters
name:String

The name of the property to exclude

Returns:Example

this, for method chaining

See Also
setPropertySelector
excludeZeroesback to summary
public Example excludeZeroes()

Exclude zero-valued properties. Equivalent to calling setPropertySelector passing in NotNullOrZeroPropertySelector#INSTANCE

Returns:Example

this, for method chaining

See Also
setPropertySelector
getEntityModeback to summary
private EntityMode getEntityMode(Criteria criteria, CriteriaQuery criteriaQuery)
getTypedValuesback to summary
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)

Implements org.hibernate.criterion.Criterion.getTypedValues.

Doc from org.hibernate.criterion.Criterion.getTypedValues.

Return typed values for all parameters in the rendered SQL fragment

Parameters
criteria:Criteria

The local criteria

criteriaQuery:CriteriaQuery

The overall criteria query

Returns:TypedValue[]

The types values (for binding)

Annotations
@Override
ignoreCaseback to summary
public Example ignoreCase()

Ignore case for all string-valued properties

Returns:Example

this, for method chaining

isPropertyIncludedback to summary
private boolean isPropertyIncluded(Object value, String name, Type type)
Annotations
@SuppressWarnings:SimplifiableIfStatement
setEscapeCharacterback to summary
public Example setEscapeCharacter(Character escapeCharacter)

Set escape character for "like" clause if like matching was enabled

Parameters
escapeCharacter:Character

The escape character

Returns:Example

this, for method chaining

See Also
enableLike
setPropertySelectorback to summary
public Example setPropertySelector(Example.PropertySelector selector)

Set the property selector to use. The property selector operates separately from excluding a property.

Parameters
selector:Example.PropertySelector

The selector to use

Returns:Example

this, for method chaining

See Also
excludeProperty
toSqlStringback to summary
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)

Implements org.hibernate.criterion.Criterion.toSqlString.

Doc from org.hibernate.criterion.Criterion.toSqlString.

Render the SQL fragment

Parameters
criteria:Criteria

The local criteria

criteriaQuery:CriteriaQuery

The overall criteria query

Returns:String

The generated SQL fragment

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.hibernate.criterion back to summary

public final Class Example.AllPropertySelector

extends Object
implements PropertySelector
Class Inheritance
All Implemented Interfaces
org.hibernate.criterion.Example.PropertySelector, java.io.Serializable

Property selector that includes all properties

Field Summary

Modifier and TypeField and Description
public static final Example.AllPropertySelector
INSTANCE

Singleton access

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
include(Object
The property value (from the example bean)
object
,
String
The name of the property
propertyName
,
Type
The type of the property
type
)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.
private Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
public static final Example.AllPropertySelector INSTANCE

Singleton access

Constructor Detail

AllPropertySelectorback to summary
public AllPropertySelector()

Method Detail

includeback to summary
public boolean include(Object object, String propertyName, Type type)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Doc from org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.

Parameters
object:Object

The property value (from the example bean)

propertyName:String

The name of the property

type:Type

The type of the property

Returns:boolean

true indicates the property should be included; false indicates it should not.

Annotations
@Override
readResolveback to summary
private Object readResolve()
org.hibernate.criterion back to summary

public final Class Example.NotNullOrZeroPropertySelector

extends Object
implements PropertySelector
Class Inheritance
  • java.lang.Object
  • org.hibernate.criterion.Example.NotNullOrZeroPropertySelector
All Implemented Interfaces
org.hibernate.criterion.Example.PropertySelector, java.io.Serializable

Property selector that includes only properties that are not null and non-zero (if numeric)

Field Summary

Modifier and TypeField and Description
public static final Example.NotNullOrZeroPropertySelector
INSTANCE

Singleton access

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
include(Object
The property value (from the example bean)
object
,
String
The name of the property
propertyName
,
Type
The type of the property
type
)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.
private Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
public static final Example.NotNullOrZeroPropertySelector INSTANCE

Singleton access

Constructor Detail

NotNullOrZeroPropertySelectorback to summary
public NotNullOrZeroPropertySelector()

Method Detail

includeback to summary
public boolean include(Object object, String propertyName, Type type)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Doc from org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.

Parameters
object:Object

The property value (from the example bean)

propertyName:String

The name of the property

type:Type

The type of the property

Returns:boolean

true indicates the property should be included; false indicates it should not.

Annotations
@Override
readResolveback to summary
private Object readResolve()
org.hibernate.criterion back to summary

public final Class Example.NotNullPropertySelector

extends Object
implements PropertySelector
Class Inheritance
All Implemented Interfaces
org.hibernate.criterion.Example.PropertySelector, java.io.Serializable

Property selector that includes only properties that are not null

Field Summary

Modifier and TypeField and Description
public static final Example.NotNullPropertySelector
INSTANCE

Singleton access

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
include(Object
The property value (from the example bean)
object
,
String
The name of the property
propertyName
,
Type
The type of the property
type
)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.
private Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
public static final Example.NotNullPropertySelector INSTANCE

Singleton access

Constructor Detail

NotNullPropertySelectorback to summary
public NotNullPropertySelector()

Method Detail

includeback to summary
public boolean include(Object object, String propertyName, Type type)

Implements org.hibernate.criterion.Example.PropertySelector.include.

Doc from org.hibernate.criterion.Example.PropertySelector.include.

Determine whether the given property should be used in the criteria.

Parameters
object:Object

The property value (from the example bean)

propertyName:String

The name of the property

type:Type

The type of the property

Returns:boolean

true indicates the property should be included; false indicates it should not.

Annotations
@Override
readResolveback to summary
private Object readResolve()
org.hibernate.criterion back to summary

public Interface Example.PropertySelector

extends Serializable
Known Direct Implementers
org.hibernate.criterion.Example.AllPropertySelector, org.hibernate.criterion.Example.NotNullPropertySelector, org.hibernate.criterion.Example.NotNullOrZeroPropertySelector

A strategy for choosing property values for inclusion in the query criteria. Note that property selection (for inclusion) operates separately from excluding a property. Excluded properties are not even passed in to the PropertySelector for consideration.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true indicates the property should be included; false indicates it should not.
include
(Object
The property value (from the example bean)
propertyValue
,
String
The name of the property
propertyName
,
Type
The type of the property
type
)

Determine whether the given property should be used in the criteria.

Method Detail

includeback to summary
public boolean include(Object propertyValue, String propertyName, Type type)

Determine whether the given property should be used in the criteria.

Parameters
propertyValue:Object

The property value (from the example bean)

propertyName:String

The name of the property

type:Type

The type of the property

Returns:boolean

true indicates the property should be included; false indicates it should not.