Search in sources :

Example 41 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class CriteriaJoinWalker method generateTableAlias.

@Override
protected String generateTableAlias(int n, PropertyPath path, Joinable joinable) {
    // TODO: deal with side-effects (changes to includeInResultRowList, userAliasList, resultTypeList)!!!
    // for collection-of-entity, we are called twice for given "path"
    // once for the collection Joinable, once for the entity Joinable.
    // the second call will/must "consume" the alias + perform side effects according to consumesEntityAlias()
    // for collection-of-other, however, there is only one call 
    // it must "consume" the alias + perform side effects, despite what consumeEntityAlias() return says
    // 
    // note: the logic for adding to the userAliasList is still strictly based on consumesEntityAlias return value
    boolean checkForSqlAlias = joinable.consumesEntityAlias();
    if (!checkForSqlAlias && joinable.isCollection()) {
        // is it a collection-of-other (component or value) ?
        CollectionPersister collectionPersister = (CollectionPersister) joinable;
        Type elementType = collectionPersister.getElementType();
        if (elementType.isComponentType() || !elementType.isEntityType()) {
            checkForSqlAlias = true;
        }
    }
    String sqlAlias = null;
    if (checkForSqlAlias) {
        final Criteria subcriteria = translator.getCriteria(path.getFullPath());
        sqlAlias = subcriteria == null ? null : translator.getSQLAlias(subcriteria);
        if (joinable.consumesEntityAlias() && !translator.hasProjection()) {
            includeInResultRowList.add(subcriteria != null && subcriteria.getAlias() != null);
            if (sqlAlias != null) {
                if (subcriteria.getAlias() != null) {
                    userAliasList.add(subcriteria.getAlias());
                    resultTypeList.add(translator.getResultType(subcriteria));
                }
            }
        }
    }
    if (sqlAlias == null) {
        sqlAlias = super.generateTableAlias(n + translator.getSQLAliasCount(), path, joinable);
    }
    return sqlAlias;
}
Also used : JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Criteria(org.hibernate.Criteria)

Example 42 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class CriteriaQueryTranslator method createCriteriaEntityNameMap.

private void createCriteriaEntityNameMap() {
    // initialize the rootProvider first
    final CriteriaInfoProvider rootProvider = new EntityCriteriaInfoProvider((Queryable) sessionFactory.getEntityPersister(rootEntityName));
    criteriaInfoMap.put(rootCriteria, rootProvider);
    nameCriteriaInfoMap.put(rootProvider.getName(), rootProvider);
    for (final String key : associationPathCriteriaMap.keySet()) {
        final Criteria value = associationPathCriteriaMap.get(key);
        final CriteriaInfoProvider info = getPathInfo(key);
        criteriaInfoMap.put(value, info);
        nameCriteriaInfoMap.put(info.getName(), info);
    }
}
Also used : Criteria(org.hibernate.Criteria)

Example 43 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class CriteriaQueryTranslator method getEntityName.

//TODO: use these in methods above
@Override
public String getEntityName(Criteria subcriteria, String propertyName) {
    if (propertyName.indexOf('.') > 0) {
        final String root = StringHelper.root(propertyName);
        final Criteria crit = getAliasedCriteria(root);
        if (crit != null) {
            return getEntityName(crit);
        }
    }
    return getEntityName(subcriteria);
}
Also used : Criteria(org.hibernate.Criteria)

Example 44 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class CriteriaQueryTranslator method getWholeAssociationPath.

private String getWholeAssociationPath(CriteriaImpl.Subcriteria subcriteria) {
    String path = subcriteria.getPath();
    // some messy, complex stuff here, since createCriteria() can take an
    // aliased path, or a path rooted at the creating criteria instance
    Criteria parent = null;
    if (path.indexOf('.') > 0) {
        // if it is a compound path
        String testAlias = StringHelper.root(path);
        if (!testAlias.equals(subcriteria.getAlias())) {
            // and the qualifier is not the alias of this criteria
            //      -> check to see if we belong to some criteria other
            //          than the one that created us
            parent = aliasCriteriaMap.get(testAlias);
        }
    }
    if (parent == null) {
        // otherwise assume the parent is the the criteria that created us
        parent = subcriteria.getParent();
    } else {
        path = StringHelper.unroot(path);
    }
    if (parent.equals(rootCriteria)) {
        // if its the root criteria, we are done
        return path;
    } else {
        // otherwise, recurse
        return getWholeAssociationPath((CriteriaImpl.Subcriteria) parent) + '.' + path;
    }
}
Also used : CriteriaImpl(org.hibernate.internal.CriteriaImpl) Criteria(org.hibernate.Criteria)

Example 45 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class NaturalIdInvalidationTest method getCitizenWithCriteria.

private void getCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        criteria.list();
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Criteria(org.hibernate.Criteria)

Aggregations

Criteria (org.hibernate.Criteria)180 Session (org.hibernate.Session)95 Test (org.junit.Test)69 List (java.util.List)39 Transaction (org.hibernate.Transaction)39 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)27 ArrayList (java.util.ArrayList)15 TestForIssue (org.hibernate.testing.TestForIssue)12 Iterator (java.util.Iterator)9 Period (org.hisp.dhis.period.Period)8 Map (java.util.Map)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)5 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)5 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 HashMap (java.util.HashMap)2