t = s.beginTransaction(); Master m1 = (Master) s.createCriteria(Master.class) .add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 ); assertTrue( m1==null ); m1 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeProperty("bigDecimal") ) .createCriteria("otherMaster") .add( Example.create(m).excludeZeroes().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 ); Master m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m2==m1 ); m.setName(null); m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( null == m2 );
/** * Exclude zero-valued properties. * * Equivalent to calling {@link #setPropertySelector} passing in {@link NotNullOrZeroPropertySelector#INSTANCE} * * @return {@code this}, for method chaining * * @see #setPropertySelector */ public Example excludeZeroes() { setPropertySelector( NotNullOrZeroPropertySelector.INSTANCE ); return this; }
/** * Create a new Example criterion instance, which includes all non-null properties by default * * @param exampleEntity The example bean to use. * * @return a new instance of Example */ public static Example create(Object exampleEntity) { if ( exampleEntity == null ) { throw new NullPointerException( "null example entity" ); } return new Example( exampleEntity, NotNullPropertySelector.INSTANCE ); }
@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 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(); }
@Override @SuppressWarnings({"unchecked", "deprecation"}) public <T> List<T> findByExample( @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); })); }
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; }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List<T> findByExample(final T exampleInstance, final String... excludeProperty) { Criteria crit = getSession().createCriteria(getPersistentClass()); Example example = Example.create(exampleInstance); for (String exclude : excludeProperty) { example.excludeProperty(exclude); } crit.add(example); return crit.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); example.setEscapeCharacter(QueryParameter.ESCAPE_CHARACTER); example = example.excludeNone(); for (final String property : qp.getExcludedProperties()) { example = example.excludeProperty(property); list.add(Example.create(filter));
public Object doInHibernate(Session session) throws HibernateException, SQLException { Example example = Example.create(arg).ignoreCase(); Criteria crit = session.createCriteria(clazz); getHibernateTemplate().applySettings(crit); crit.add(example); Map argsMap = (arguments.length > 1 && (arguments[1] instanceof Map)) ? (Map) arguments[1] : Collections.EMPTY_MAP; GrailsHibernateUtil.populateArgumentsForCriteria(application,clazz, crit, argsMap); return crit.list(); } });
/** * 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 ); }
@Override @SuppressWarnings("unchecked") public List<ISoapBoxEntity> find(ISoapBoxEntity entity) { EntityManager manager = ConnectionDB.getManager(); manager.clear(); Session sessao = (Session) manager.getDelegate(); Example example = Example.create(entity); example.excludeZeroes(); Criteria criteria = sessao.createCriteria(entity.getClass()); criteria.add(example); return criteria.list(); }
Example ex = new Example(); ex.setUp(); ex.testExample(); ex.isElementPresent(); ex.tearDown();
@Override public void performExecute() throws PersistenceException { Example example = createExample(); if (excludeProperties != null) { for (int i = 0; i < excludeProperties.length; i++) { example.excludeProperty(excludeProperties[i]); } } DetachedCriteria crit = createCriteria(example); if (orderBy != null) { if (orderByAsc) { crit.addOrder(Order.asc(orderBy)); } else { crit.addOrder(Order.desc(orderBy)); } } Session hibernateSession = HibernateSessionHelper.getHibernateSession(getEntityManager()); final Criteria criteria = crit.getExecutableCriteria(hibernateSession); prepareCache(criteria); result = executeFind(criteria); }
public void testRemoveInner() { Example.Inner[] inner = new Example.Inner(45); Example e = new Example(); e.addInner(inner); e.addInner(inner); e.removeInner(inner); assertEquals(0, e.getInners().size()); }
JSONParser parser = new JSONParser(); ArrayList list = parser.parse(ArrayList.class, json); List<Example> result = new ArrayList<Example>(); for(int i = 0 ; i < list.size() ; i++){ HashMap<String, String> map = (HashMap) list.get(i); Example example = new Example(); example.setFoo(map.get("foo")); example.setBar(map.get("bar")); example.setFubar(map.get("fubar")); result.add(example); }
String a = "Hello"; String b = "World"; // For the duration of the constructor, a becomes firstName, // and b becomes secondName Example e = new Example(a, b); // Once the constructor exists, firstName and secondName disappear. // Resetting a and b after the call is no longer relevant a = "Quick brown fox jumps"; b = "over the lazy dog"; System.out.println(e.getName()+" "+e.getSurname());
public static void main(String[] args) { Example sharedData = new Example(); for (int i = 0; i < 1000; i++) sharedData.increment(); System.out.println("Incrementer finished"); for (int i = 0; i < 1000; i++) sharedData.decrement(); System.out.println("Decrementer finished"); System.out.println(sharedData.count); }