/** * Use the "like" operator for all string-valued properties. This form implicitly uses {@link MatchMode#EXACT} * * @return {@code this}, for method chaining */ public Example enableLike() { return enableLike( MatchMode.EXACT ); }
@Test public void testSimpleQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", "open sourc%", "open source1"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike(); crit.add(ex); List result = crit.list(); assertNotNull(result); assertEquals(1, result.size()); t.commit(); s.close(); }
@Test public void testExcludingQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", null, "ope%"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike() .excludeProperty("component.subComponent"); crit.add(ex); List result = crit.list(); assertNotNull(result); assertEquals(3, result.size()); master = getMaster("hibernate", "ORM tool", "fake stuff"); crit = s.createCriteria(Componentizable.class); ex = Example.create(master).enableLike() .excludeProperty("component.subComponent.subName1"); crit.add(ex); result = crit.list(); assertNotNull(result); assertEquals(1, result.size()); t.commit(); s.close(); }
@Test @SkipForDialect( value = SybaseASE15Dialect.class, jiraKey = "HHH-4580") public void testJunctionNotExpressionQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", null, "ope%"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike(); crit.add(Restrictions.or(Restrictions.not(ex), ex)); List result = crit.list(); assertNotNull(result); assertEquals(2, result.size()); t.commit(); s.close(); }
t = s.beginTransaction(); Master m1 = (Master) s.createCriteria(Master.class) .add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 );
Example.create(example) .excludeZeroes() .enableLike(MatchMode.ANYWHERE) .excludeProperty("bool") .excludeProperty("char")
public List findByExample(Object example, String condition, boolean enableLike) { Criteria ec = this.getCurrentSession().createCriteria(example.getClass()); if (enableLike) ec.add(Example.create(example).enableLike()); else ec.add(Example.create(example)); if (condition != null && !condition.equals("")) { String newCondition = condition.replaceAll("`", "'"); ec.add(Restrictions.sqlRestriction(newCondition)); } return ec.list(); }
public Object getMaxByExample(final Object exampleEntity, final String maxProperty, final String condition, final boolean enableLike) { Criteria executableCriteria = this.getCurrentSession().createCriteria(exampleEntity.getClass()); executableCriteria.setProjection(Projections.max(maxProperty)); if (enableLike) { executableCriteria.add(Example.create(exampleEntity).enableLike()); } else { executableCriteria.add(Example.create(exampleEntity)); } if (condition != null && !condition.equals("")) { String newCondition = condition.replaceAll("`", "'"); executableCriteria.add(Restrictions.sqlRestriction(newCondition)); } return executableCriteria.uniqueResult(); } }
/** * Use the "like" operator for all string-valued properties */ public Example enableLike() { return enableLike(MatchMode.EXACT); }
/** * Use the "like" operator for all string-valued properties */ public Example enableLike() { return enableLike(MatchMode.EXACT); }
/** * Use the "like" operator for all string-valued properties */ public Example enableLike() { return enableLike(MatchMode.EXACT); }
/** * Use the "like" operator for all string-valued properties */ public Example enableLike() { return enableLike(MatchMode.EXACT); }
protected Example createExample() { return Example.create(exampleInstance).excludeZeroes().ignoreCase().enableLike(); }
public static Example getExampleCriterion(Object entity, String[] excludePropertes, MatchMode mode) { Example example = Example.create(entity).setPropertySelector(new NotEmptyPropertySelector()); if (null != mode) { example.enableLike(mode); } if (null != excludePropertes) { for (int i = 0; i < excludePropertes.length; i++) { example.excludeProperty(excludePropertes[i]); } } return example; }
public NakedObject[] getInstancesByPatternCriteria(final PatternCriteria criteria) { try { boolean started = startHibernateTransaction(); Object pojoPattern = criteria.getPattern().getObject(); // ignore title when pattern matching as it cannot be null (filled in by adapter) final Criteria hibernateCriteria = HibernateUtil.getCurrentSession().createCriteria(pojoPattern.getClass()).add( Example.create(pojoPattern).ignoreCase().enableLike(MatchMode.ANYWHERE).excludeProperty("title")); if (!criteria.includeSubclasses()) { final ClassMetadata meta = HibernateUtil.getSessionFactory().getClassMetadata(pojoPattern.getClass().getName()); if (meta.hasSubclasses()) { hibernateCriteria.add(Restrictions.eq("class", pojoPattern.getClass())); } } final List results = hibernateCriteria.list(); final NakedObject[] loadedObjects = loadObjects(HibernateUtil.getCurrentSession(), criteria.getSpecification(), results); commitHibernateTransaction(started); return loadedObjects; } catch (final RuntimeException e) { HibernateUtil.rollbackTransaction(); throw e; } }
@SuppressWarnings("unchecked") @Override public Collection<T> doInHibernate(Session session) throws HibernateException, SQLException { Example e = Example.create(this.parameter.getExample()).enableLike(MatchMode.ANYWHERE); Criteria criteria = session.createCriteria(this.clazz); criteria.add(e); if (this.parameter.isUnique()){ criteria.setMaxResults(1); }else if (this.parameter.getLimit() > 0){ criteria.setFirstResult(this.parameter.getStart()); criteria.setMaxResults(this.parameter.getLimit() + 1); } if (this.parameter.isDistanct()) criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); return criteria.list(); }
Example example = Example.create(filter); if (qp.getFilterState() == QueryParameter.FILTER_STATE_IGNORECASE_ENABLELIKE) { list.add(example.enableLike(getMatchMode(qp)).ignoreCase()); } else if (qp.getFilterState() == QueryParameter.FILTER_STATE_IGNORECASE_NOENABLELIKE) { list.add(example.ignoreCase()); } else if (qp.getFilterState() == QueryParameter.FILTER_STATE_NOIGNORECASE_ENABLELIKE) { list.add(example.enableLike(getMatchMode(qp))); } else { list.add(example);
@Override public <T extends Entity, E extends T> List<T> findByExample(final E example, final ExampleSettings<T> settings) { Example theExample = Example.create(example); if (settings.isLikeEnabled()) { theExample.enableLike(MatchMode.ANYWHERE); } if (settings.isIgnoreCaseEnabled()) { theExample.ignoreCase(); } if (settings.isExcludeNone()) { theExample.excludeNone(); } if (settings.isExcludeZeroes()) { theExample.excludeZeroes(); } for (String propName : settings.getExcludedProperties()) { theExample.excludeProperty(propName); } return getSession().createCriteria(settings.getEntityClass()).add(theExample).list(); }
private static org.hibernate.criterion.Example adaptExample(final Example expr) { final Object entity = ExampleV2Reflection.getEntity(expr); final org.hibernate.criterion.Example.PropertySelector selector = ExamplePropertySelectorV2Adapter.adapt(ExampleV2Reflection.getSelector(expr)); final org.hibernate.criterion.Example example = org.hibernate.criterion.Example.create(entity); example.setPropertySelector(selector); for (final Object propertyName : ExampleV2Reflection.getExcludedProperties(expr)) { example.excludeProperty((String) propertyName); } if (ExampleV2Reflection.getIsLikeEnabled(expr)) { example.enableLike(MatchModeV2Adapter.adapt(ExampleV2Reflection.getMatchMode(expr))); } return example; }