Package org.hibernate.criterion
Class Example
- java.lang.Object
-
- org.hibernate.criterion.Example
-
- All Implemented Interfaces:
java.io.Serializable
,Criterion
public class Example extends java.lang.Object implements Criterion
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.- See Also:
Criteria
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Example.AllPropertySelector
Property selector that includes all propertiesstatic class
Example.NotNullOrZeroPropertySelector
Property selector that includes only properties that are notnull
and non-zero (if numeric)static class
Example.NotNullPropertySelector
Property selector that includes only properties that are notnull
static interface
Example.PropertySelector
A strategy for choosing property values for inclusion in the query criteria.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Example(java.lang.Object exampleEntity, Example.PropertySelector selector)
Allow subclasses to instantiate as needed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addComponentTypedValues(java.lang.String path, java.lang.Object component, CompositeType type, java.util.List<TypedValue> list, Criteria criteria, CriteriaQuery criteriaQuery)
protected void
addPropertyTypedValue(java.lang.Object value, Type type, java.util.List<TypedValue> list)
protected void
appendComponentCondition(java.lang.String path, java.lang.Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, java.lang.StringBuilder buf)
protected void
appendPropertyCondition(java.lang.String propertyName, java.lang.Object propertyValue, Criteria criteria, CriteriaQuery cq, java.lang.StringBuilder buf)
static Example
create(java.lang.Object exampleEntity)
Create a new Example criterion instance, which includes all non-null properties by defaultExample
enableLike()
Use the "like" operator for all string-valued properties.Example
enableLike(MatchMode matchMode)
Use the "like" operator for all string-valued propertiesExample
excludeNone()
Include all properties.Example
excludeProperty(java.lang.String name)
Exclude a particular property by name.Example
excludeZeroes()
Exclude zero-valued properties.TypedValue[]
getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
Return typed values for all parameters in the rendered SQL fragmentExample
ignoreCase()
Ignore case for all string-valued propertiesExample
setEscapeCharacter(java.lang.Character escapeCharacter)
Set escape character for "like" clause if like matching was enabledExample
setPropertySelector(Example.PropertySelector selector)
Set the property selector to use.java.lang.String
toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Render the SQL fragmentjava.lang.String
toString()
-
-
-
Constructor Detail
-
Example
protected Example(java.lang.Object exampleEntity, Example.PropertySelector selector)
Allow subclasses to instantiate as needed.- Parameters:
exampleEntity
- The example beanselector
- The property selector to use
-
-
Method Detail
-
create
public static Example create(java.lang.Object exampleEntity)
Create a new Example criterion instance, which includes all non-null properties by default- Parameters:
exampleEntity
- The example bean to use.- Returns:
- a new instance of Example
-
setEscapeCharacter
public Example setEscapeCharacter(java.lang.Character escapeCharacter)
Set escape character for "like" clause if like matching was enabled- Parameters:
escapeCharacter
- The escape character- Returns:
this
, for method chaining- See Also:
enableLike()
-
enableLike
public Example enableLike()
Use the "like" operator for all string-valued properties. This form implicitly usesMatchMode.EXACT
- Returns:
this
, for method chaining
-
enableLike
public Example enableLike(MatchMode matchMode)
Use the "like" operator for all string-valued properties- Parameters:
matchMode
- The match mode to use.- Returns:
this
, for method chaining
-
ignoreCase
public Example ignoreCase()
Ignore case for all string-valued properties- Returns:
this
, for method chaining
-
setPropertySelector
public Example setPropertySelector(Example.PropertySelector selector)
Set the property selector to use. The property selector operates separate from excluding a property.- Parameters:
selector
- The selector to use- Returns:
this
, for method chaining- See Also:
excludeProperty(java.lang.String)
-
excludeZeroes
public Example excludeZeroes()
Exclude zero-valued properties. Equivalent to callingsetPropertySelector(org.hibernate.criterion.Example.PropertySelector)
passing inExample.NotNullOrZeroPropertySelector.INSTANCE
- Returns:
this
, for method chaining- See Also:
setPropertySelector(org.hibernate.criterion.Example.PropertySelector)
-
excludeNone
public Example excludeNone()
Include all properties. Equivalent to callingsetPropertySelector(org.hibernate.criterion.Example.PropertySelector)
passing inExample.AllPropertySelector.INSTANCE
- Returns:
this
, for method chaining- See Also:
setPropertySelector(org.hibernate.criterion.Example.PropertySelector)
-
excludeProperty
public Example excludeProperty(java.lang.String name)
Exclude a particular property by name.- Parameters:
name
- The name of the property to exclude- Returns:
this
, for method chaining- See Also:
setPropertySelector(org.hibernate.criterion.Example.PropertySelector)
-
toSqlString
public java.lang.String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Description copied from interface:Criterion
Render the SQL fragment- Specified by:
toSqlString
in interfaceCriterion
- Parameters:
criteria
- The local criteriacriteriaQuery
- The overal criteria query- Returns:
- The generated SQL fragment
-
getTypedValues
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
Description copied from interface:Criterion
Return typed values for all parameters in the rendered SQL fragment- Specified by:
getTypedValues
in interfaceCriterion
- Parameters:
criteria
- The local criteriacriteriaQuery
- The overal criteria query- Returns:
- The types values (for binding)
-
addPropertyTypedValue
protected void addPropertyTypedValue(java.lang.Object value, Type type, java.util.List<TypedValue> list)
-
addComponentTypedValues
protected void addComponentTypedValues(java.lang.String path, java.lang.Object component, CompositeType type, java.util.List<TypedValue> list, Criteria criteria, CriteriaQuery criteriaQuery)
-
appendPropertyCondition
protected void appendPropertyCondition(java.lang.String propertyName, java.lang.Object propertyValue, Criteria criteria, CriteriaQuery cq, java.lang.StringBuilder buf)
-
appendComponentCondition
protected void appendComponentCondition(java.lang.String path, java.lang.Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, java.lang.StringBuilder buf)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-