Search in sources :

Example 16 with Criterion

use of org.hibernate.criterion.Criterion in project opennms by OpenNMS.

the class CategoryDaoHibernate method getCriterionForCategorySetsUnion.

/**
 * <p>getCriterionForCategorySetsUnion</p>
 *
 * @param categories an array of {@link java.lang.String} objects.
 * @return a {@link java.util.List} object.
 */
@Override
public List<Criterion> getCriterionForCategorySetsUnion(String[]... categories) {
    Assert.notNull(categories, "categories argument must not be null");
    Assert.isTrue(categories.length >= 1, "categories must have at least one set of categories");
    // Build a list a list of category IDs to use when building the restrictions
    List<List<Integer>> categoryIdsList = new ArrayList<List<Integer>>(categories.length);
    for (String[] categoryStrings : categories) {
        List<Integer> categoryIds = new ArrayList<Integer>(categoryStrings.length);
        for (String categoryString : categoryStrings) {
            OnmsCategory category = findByName(categoryString);
            if (category == null) {
                throw new IllegalArgumentException("Could not find category for name '" + categoryString + "'");
            }
            categoryIds.add(category.getId());
        }
        categoryIdsList.add(categoryIds);
    }
    List<Criterion> criteria = new ArrayList<Criterion>(categoryIdsList.size());
    for (List<Integer> categoryIds : categoryIdsList) {
        Type[] types = new Type[categoryIds.size()];
        String[] questionMarks = new String[categoryIds.size()];
        Type theOneAndOnlyType = new IntegerType();
        for (int i = 0; i < categoryIds.size(); i++) {
            types[i] = theOneAndOnlyType;
            questionMarks[i] = "?";
        }
        String sql = "{alias}.nodeId in (select distinct cn.nodeId from category_node cn where cn.categoryId in (" + StringUtils.arrayToCommaDelimitedString(questionMarks) + "))";
        criteria.add(Restrictions.sqlRestriction(sql, categoryIds.toArray(new Integer[categoryIds.size()]), types));
    }
    return criteria;
}
Also used : ArrayList(java.util.ArrayList) IntegerType(org.hibernate.type.IntegerType) IntegerType(org.hibernate.type.IntegerType) StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Criterion(org.hibernate.criterion.Criterion) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with Criterion

use of org.hibernate.criterion.Criterion in project dhis2-core by dhis2.

the class CriteriaQueryEngine method addCriterion.

private void addCriterion(DetachedCriteria criteria, org.hisp.dhis.query.Criterion criterion) {
    if (Restriction.class.isInstance(criterion)) {
        Restriction restriction = (Restriction) criterion;
        Criterion hibernateCriterion = getHibernateCriterion(restriction);
        if (hibernateCriterion != null) {
            criteria.add(hibernateCriterion);
        }
    } else if (Junction.class.isInstance(criterion)) {
        org.hibernate.criterion.Junction junction = null;
        if (Disjunction.class.isInstance(criterion)) {
            junction = Restrictions.disjunction();
        } else if (Conjunction.class.isInstance(criterion)) {
            junction = Restrictions.conjunction();
        }
        criteria.add(junction);
        for (org.hisp.dhis.query.Criterion c : ((Junction) criterion).getCriterions()) {
            addJunction(junction, c);
        }
    }
}
Also used : Criterion(org.hibernate.criterion.Criterion)

Example 18 with Criterion

use of org.hibernate.criterion.Criterion in project hibernate-orm by hibernate.

the class CriterionTest method testIlikeRendering.

@Test
public void testIlikeRendering() {
    SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, IlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
    try {
        final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
        final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
        final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
        final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
        assertEquals("a.name insensitiveLike ?", ilikeExpressionSqlFragment);
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Criterion(org.hibernate.criterion.Criterion) IrrelevantEntity(org.hibernate.IrrelevantEntity) Criteria(org.hibernate.Criteria) CriteriaQueryTranslator(org.hibernate.loader.criteria.CriteriaQueryTranslator) Test(org.junit.Test)

Example 19 with Criterion

use of org.hibernate.criterion.Criterion in project hibernate-orm by hibernate.

the class CriterionTest method testIlikeMimicing.

@Test
public void testIlikeMimicing() {
    SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, NonIlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
    try {
        final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
        final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
        final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
        final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
        assertEquals("lowLowLow(a.name) like ?", ilikeExpressionSqlFragment);
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Criterion(org.hibernate.criterion.Criterion) IrrelevantEntity(org.hibernate.IrrelevantEntity) Criteria(org.hibernate.Criteria) CriteriaQueryTranslator(org.hibernate.loader.criteria.CriteriaQueryTranslator) Test(org.junit.Test)

Example 20 with Criterion

use of org.hibernate.criterion.Criterion in project hibernate-orm by hibernate.

the class TestSpatialRestrictions method contains.

@Test
public void contains() throws SQLException {
    if (!isSupportedByDialect(SpatialFunction.contains)) {
        return;
    }
    Map<Integer, Boolean> dbexpected = expectationsFactory.getContains(expectationsFactory.getTestPolygon());
    Criterion spatialCriterion = SpatialRestrictions.contains("geom", expectationsFactory.getTestPolygon());
    retrieveAndCompare(dbexpected, spatialCriterion);
}
Also used : Criterion(org.hibernate.criterion.Criterion) Test(org.junit.Test)

Aggregations

Criterion (org.hibernate.criterion.Criterion)27 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 List (java.util.List)6 RootMap (com.cosylab.cdb.jdal.hibernate.RootMap)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 Session (org.hibernate.Session)5 Criteria (org.hibernate.Criteria)4 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 Component (alma.acs.tmcdb.Component)2 Container (alma.acs.tmcdb.Container)2 Comparator (java.util.Comparator)2 IrrelevantEntity (org.hibernate.IrrelevantEntity)2 SessionFactory (org.hibernate.SessionFactory)2 Configuration (org.hibernate.cfg.Configuration)2 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)2