@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(); }
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 );
.excludeZeroes() .ignoreCase() .excludeProperty("bool") .excludeProperty("char") .excludeProperty("yesno") .excludeZeroes() .enableLike(MatchMode.ANYWHERE) .excludeProperty("bool") .excludeProperty("char") .excludeProperty("yesno")
/** * {@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(); }
@Override public Connection getConnection(PatientSimilarityView patientPair) { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(Connection.class); Connection connection = new Connection(this.publicPatientSimilarityViewFactory.convert(patientPair)); c.add(Example.create(connection).excludeProperty("id")); @SuppressWarnings("unchecked") List<Connection> foundEntries = c.list(); if (foundEntries.isEmpty()) { Transaction t = session.beginTransaction(); t.begin(); session.save(connection); t.commit(); return connection; } return foundEntries.get(0); }
/** * FindByExample. * * @param exampleInstance to use * @param excludeProperty to use * @return List of matching objects */ @SuppressWarnings("unchecked") public List<T> findByExample(T exampleInstance, 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(); } }
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[] getPerspectiveInstances(final PerspectiveCriteria criteria) { try { boolean started = startHibernateTransaction(); final Perspective pojoPattern = new Perspective(); pojoPattern.setUserName(criteria.getUserName()); // 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).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; } }
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; } }
@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(); }
example = example.excludeNone(); for (final String property : qp.getExcludedProperties()) { example = example.excludeProperty(property);
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; }
@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); }