Search in sources :

Example 16 with DetachedCriteria

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

the class HibernateGenericStore method getCriteria.

/**
     * Creates a Criteria for the implementation Class type.
     * <p>
     * Please note that sharing is not considered.
     *
     * @return a Criteria instance.
     */
@Override
public final Criteria getCriteria() {
    DetachedCriteria criteria = DetachedCriteria.forClass(getClazz());
    preProcessDetachedCriteria(criteria);
    return getExecutableCriteria(criteria);
}
Also used : DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 17 with DetachedCriteria

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

the class CriteriaQueryEngine method query.

@Override
@SuppressWarnings("unchecked")
public List<T> query(Query query) {
    Schema schema = query.getSchema();
    InternalHibernateGenericStore<?> store = getStore((Class<? extends IdentifiableObject>) schema.getKlass());
    if (store == null) {
        return new ArrayList<>();
    }
    if (query.getUser() == null) {
        query.setUser(currentUserService.getCurrentUser());
    }
    if (!query.isPlannedQuery()) {
        QueryPlan queryPlan = queryPlanner.planQuery(query, true);
        query = queryPlan.getPersistedQuery();
    }
    DetachedCriteria detachedCriteria = buildCriteria(store.getSharingDetachedCriteria(query.getUser()), query);
    Criteria criteria = store.getCriteria();
    if (criteria == null) {
        return new ArrayList<>();
    }
    criteria.setFirstResult(query.getFirstResult());
    criteria.setMaxResults(query.getMaxResults());
    for (Order order : query.getOrders()) {
        criteria.addOrder(getHibernateOrder(order));
    }
    return criteria.add(Subqueries.propertyIn("id", detachedCriteria)).list();
}
Also used : Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) QueryPlan(org.hisp.dhis.query.planner.QueryPlan)

Example 18 with DetachedCriteria

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

the class CriteriaQueryEngine method count.

@Override
@SuppressWarnings("unchecked")
public int count(Query query) {
    Schema schema = query.getSchema();
    InternalHibernateGenericStore<?> store = getStore((Class<? extends IdentifiableObject>) schema.getKlass());
    if (store == null) {
        return 0;
    }
    if (query.getUser() == null) {
        query.setUser(currentUserService.getCurrentUser());
    }
    if (!query.isPlannedQuery()) {
        QueryPlan queryPlan = queryPlanner.planQuery(query, true);
        query = queryPlan.getPersistedQuery();
    }
    DetachedCriteria detachedCriteria = buildCriteria(store.getSharingDetachedCriteria(query.getUser()), query);
    Criteria criteria = store.getCriteria();
    if (criteria == null) {
        return 0;
    }
    return ((Number) criteria.add(Subqueries.propertyIn("id", detachedCriteria)).setProjection(Projections.countDistinct("id")).uniqueResult()).intValue();
}
Also used : Schema(org.hisp.dhis.schema.Schema) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) QueryPlan(org.hisp.dhis.query.planner.QueryPlan)

Aggregations

DetachedCriteria (org.hibernate.criterion.DetachedCriteria)18 Test (org.junit.Test)13 List (java.util.List)11 Session (org.hibernate.Session)9 ArrayList (java.util.ArrayList)7 Transaction (org.hibernate.Transaction)7 Criteria (org.hibernate.Criteria)6 SkipForDialect (org.hibernate.testing.SkipForDialect)3 TestForIssue (org.hibernate.testing.TestForIssue)2 QueryPlan (org.hisp.dhis.query.planner.QueryPlan)2 Schema (org.hisp.dhis.schema.Schema)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 JDBCException (org.hibernate.JDBCException)1 QueryException (org.hibernate.QueryException)1 Disjunction (org.hibernate.criterion.Disjunction)1 ProjectionList (org.hibernate.criterion.ProjectionList)1 Oracle8iDialect (org.hibernate.dialect.Oracle8iDialect)1 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1