Examplepublic class Example extends Object implements CriterionSupport 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. |
Fields Summary |
---|
private final Object | entity | private final Set | excludedProperties | private PropertySelector | selector | private boolean | isLikeEnabled | private Character | escapeCharacter | private boolean | isIgnoreCaseEnabled | private MatchMode | matchMode | private static final PropertySelector | NOT_NULL | private static final PropertySelector | ALL | private static final PropertySelector | NOT_NULL_OR_ZERO | private static final Object[] | TYPED_VALUES |
Constructors Summary |
---|
protected Example(Object entity, PropertySelector selector)
this.entity = entity;
this.selector = selector;
|
Methods Summary |
---|
protected void | addComponentTypedValues(java.lang.String path, java.lang.Object component, org.hibernate.type.AbstractComponentType type, java.util.List list, org.hibernate.Criteria criteria, CriteriaQuery criteriaQuery)
if (component!=null) {
String[] propertyNames = type.getPropertyNames();
Type[] subtypes = type.getSubtypes();
Object[] values = type.getPropertyValues( component, getEntityMode(criteria, criteriaQuery) );
for (int i=0; i<propertyNames.length; i++) {
Object value = values[i];
Type subtype = subtypes[i];
String subpath = StringHelper.qualify( path, propertyNames[i] );
if ( isPropertyIncluded(value, subpath, subtype) ) {
if ( subtype.isComponentType() ) {
addComponentTypedValues(subpath, value, (AbstractComponentType) subtype, list, criteria, criteriaQuery);
}
else {
addPropertyTypedValue(value, subtype, list);
}
}
}
}
| protected void | addPropertyTypedValue(java.lang.Object value, org.hibernate.type.Type type, java.util.List list)
if ( value!=null ) {
if ( value instanceof String ) {
String string = (String) value;
if (isIgnoreCaseEnabled) string = string.toLowerCase();
if (isLikeEnabled) string = matchMode.toMatchString(string);
value = string;
}
list.add( new TypedValue(type, value, null) );
}
| protected void | appendComponentCondition(java.lang.String path, java.lang.Object component, org.hibernate.type.AbstractComponentType type, org.hibernate.Criteria criteria, CriteriaQuery criteriaQuery, java.lang.StringBuffer buf)
if (component!=null) {
String[] propertyNames = type.getPropertyNames();
Object[] values = type.getPropertyValues( component, getEntityMode(criteria, criteriaQuery) );
Type[] subtypes = type.getSubtypes();
for (int i=0; i<propertyNames.length; i++) {
String subpath = StringHelper.qualify( path, propertyNames[i] );
Object value = values[i];
if ( isPropertyIncluded( value, subpath, subtypes[i] ) ) {
Type subtype = subtypes[i];
if ( subtype.isComponentType() ) {
appendComponentCondition(
subpath,
value,
(AbstractComponentType) subtype,
criteria,
criteriaQuery,
buf
);
}
else {
appendPropertyCondition(
subpath,
value,
criteria,
criteriaQuery,
buf
);
}
}
}
}
| protected void | appendPropertyCondition(java.lang.String propertyName, java.lang.Object propertyValue, org.hibernate.Criteria criteria, CriteriaQuery cq, java.lang.StringBuffer buf)
Criterion crit;
if ( propertyValue!=null ) {
boolean isString = propertyValue instanceof String;
if ( isLikeEnabled && isString ) {
crit = new LikeExpression(
propertyName,
( String ) propertyValue,
matchMode,
escapeCharacter,
isIgnoreCaseEnabled
);
}
else {
crit = new SimpleExpression( propertyName, propertyValue, "=", isIgnoreCaseEnabled && isString );
}
}
else {
crit = new NullExpression(propertyName);
}
String critCondition = crit.toSqlString(criteria, cq);
if ( buf.length()>1 && critCondition.trim().length()>0 ) buf.append(" and ");
buf.append(critCondition);
| public static Example | create(java.lang.Object entity)Create a new instance, which includes all non-null properties
by default
if (entity==null) throw new NullPointerException("null example");
return new Example(entity, NOT_NULL);
| public Example | enableLike(MatchMode matchMode)Use the "like" operator for all string-valued properties
isLikeEnabled = true;
this.matchMode = matchMode;
return this;
| public Example | enableLike()Use the "like" operator for all string-valued properties
return enableLike(MatchMode.EXACT);
| public Example | excludeNone()Don't exclude null or zero-valued properties
setPropertySelector(ALL);
return this;
| public Example | excludeProperty(java.lang.String name)Exclude a particular named property
excludedProperties.add(name);
return this;
| public Example | excludeZeroes()Exclude zero-valued properties
setPropertySelector(NOT_NULL_OR_ZERO);
return this;
| private org.hibernate.EntityMode | getEntityMode(org.hibernate.Criteria criteria, CriteriaQuery criteriaQuery)
EntityPersister meta = criteriaQuery.getFactory()
.getEntityPersister( criteriaQuery.getEntityName(criteria) );
EntityMode result = meta.guessEntityMode(entity);
if (result==null) {
throw new ClassCastException( entity.getClass().getName() );
}
return result;
| public org.hibernate.engine.TypedValue[] | getTypedValues(org.hibernate.Criteria criteria, CriteriaQuery criteriaQuery)
EntityPersister meta = criteriaQuery.getFactory()
.getEntityPersister( criteriaQuery.getEntityName(criteria) );
String[] propertyNames = meta.getPropertyNames();
Type[] propertyTypes = meta.getPropertyTypes();
//TODO: get all properties, not just the fetched ones!
Object[] values = meta.getPropertyValues( entity, getEntityMode(criteria, criteriaQuery) );
List list = new ArrayList();
for (int i=0; i<propertyNames.length; i++) {
Object value = values[i];
Type type = propertyTypes[i];
String name = propertyNames[i];
boolean isPropertyIncluded = i!=meta.getVersionProperty() &&
isPropertyIncluded(value, name, type);
if (isPropertyIncluded) {
if ( propertyTypes[i].isComponentType() ) {
addComponentTypedValues(name, value, (AbstractComponentType) type, list, criteria, criteriaQuery);
}
else {
addPropertyTypedValue(value, type, list);
}
}
}
return (TypedValue[]) list.toArray(TYPED_VALUES);
| public Example | ignoreCase()Ignore case for all string-valued properties
isIgnoreCaseEnabled = true;
return this;
| private boolean | isPropertyIncluded(java.lang.Object value, java.lang.String name, org.hibernate.type.Type type)
return !excludedProperties.contains(name) &&
!type.isAssociationType() &&
selector.include(value, name, type);
| public Example | setEscapeCharacter(java.lang.Character escapeCharacter)Set escape character for "like" clause
this.escapeCharacter = escapeCharacter;
return this;
| public Example | setPropertySelector(org.hibernate.criterion.Example$PropertySelector selector)Set the property selector
this.selector = selector;
return this;
| public java.lang.String | toSqlString(org.hibernate.Criteria criteria, CriteriaQuery criteriaQuery)
StringBuffer buf = new StringBuffer().append('(");
EntityPersister meta = criteriaQuery.getFactory().getEntityPersister( criteriaQuery.getEntityName(criteria) );
String[] propertyNames = meta.getPropertyNames();
Type[] propertyTypes = meta.getPropertyTypes();
//TODO: get all properties, not just the fetched ones!
Object[] propertyValues = meta.getPropertyValues( entity, getEntityMode(criteria, criteriaQuery) );
for (int i=0; i<propertyNames.length; i++) {
Object propertyValue = propertyValues[i];
String propertyName = propertyNames[i];
boolean isPropertyIncluded = i!=meta.getVersionProperty() &&
isPropertyIncluded( propertyValue, propertyName, propertyTypes[i] );
if (isPropertyIncluded) {
if ( propertyTypes[i].isComponentType() ) {
appendComponentCondition(
propertyName,
propertyValue,
(AbstractComponentType) propertyTypes[i],
criteria,
criteriaQuery,
buf
);
}
else {
appendPropertyCondition(
propertyName,
propertyValue,
criteria,
criteriaQuery,
buf
);
}
}
}
if ( buf.length()==1 ) buf.append("1=1"); //yuck!
return buf.append(')").toString();
| public java.lang.String | toString()
return "example (" + entity + ')";
|
|