ExcludeFalse Capability Query by Example Example
------------------------------------------------
Key: HSEARCH-1113
URL:
https://hibernate.onjira.com/browse/HSEARCH-1113
Project: Hibernate Search
Issue Type: Improvement
Components: engine
Affects Versions: 4.1.0.Final
Environment: all
Reporter: Uday Kari
Priority: Minor
In web form development, checkboxes are mapped to booleans properties. When using a form
to conduct Query By Example, the results should exclude un-checked or false properties. I
have already fixed this for my application, here is the code...you are welcome to use it.
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.criterion;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.engine.TypedValue;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.CompositeType;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;
/**
* Support for query by example.
* <pre>
* List results = session.createCriteria(Parent.class)
* .add( Example.create(parent).ignoreCase() )
* .createCriteria("child")
* .add( Example.create( parent.getChild() ) )
* .list();
* </pre>
* "Examples" may be mixed and matched with "Expressions" in the same
<tt>Criteria</tt>.
* @see org.hibernate.Criteria
* @author Gavin King
*/
public class Example implements Criterion {
private final Object entity;
private final Set excludedProperties = new HashSet();
private PropertySelector selector;
private boolean isLikeEnabled;
private Character escapeCharacter;
private boolean isIgnoreCaseEnabled;
private MatchMode matchMode;
/**
* A strategy for choosing property values for inclusion in the query
* criteria
*/
public static interface PropertySelector extends Serializable {
public boolean include(Object propertyValue, String propertyName, Type type);
}
private static final PropertySelector NOT_NULL = new NotNullPropertySelector();
private static final PropertySelector ALL = new AllPropertySelector();
private static final PropertySelector NOT_NULL_OR_ZERO = new
NotNullOrZeroPropertySelector();
////////////////////////////////////////////////////////////
// Uday Edits
//
private static final PropertySelector NOT_FALSE = new NotFalsePropertySelector();
static final class NotFalsePropertySelector implements PropertySelector
{
public boolean include(Object object, String propertyName, Type type)
{
if (object == null)
{
return false;
}
else if (object instanceof Boolean)
{
return ((Boolean) object).booleanValue();
}
else
{
return true;
}
}
}
/**
* Exclude false-valued properties
*/
public Example excludeFalse() {
setPropertySelector(NOT_FALSE);
return this;
}
//
// End
/////////////////////////////////////////
[...]
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira