Search in sources :

Example 16 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 17 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 18 with Criteria

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

the class IdManyToOneTest method testCriteriaRestrictionOnIdManyToOne.

@Test
@TestForIssue(jiraKey = "HHH-7767")
public void testCriteriaRestrictionOnIdManyToOne() {
    Session s = openSession();
    s.beginTransaction();
    s.createQuery("from Course c join c.students cs join cs.student s where s.name = 'Foo'").list();
    Criteria criteria = s.createCriteria(Course.class);
    criteria.createCriteria("students").createCriteria("student").add(Restrictions.eq("name", "Foo"));
    criteria.list();
    Criteria criteria2 = s.createCriteria(Course.class);
    criteria2.createAlias("students", "cs");
    criteria2.add(Restrictions.eq("cs.value", "Bar"));
    criteria2.createAlias("cs.student", "s");
    criteria2.add(Restrictions.eq("s.name", "Foo"));
    criteria2.list();
    s.getTransaction().commit();
    s.close();
}
Also used : Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with Criteria

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

the class JoinTest method testManyToOne.

@Test
public void testManyToOne() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Life life = new Life();
    Cat cat = new Cat();
    cat.setName("kitty");
    cat.setStoryPart2("and the story continues");
    life.duration = 15;
    life.fullDescription = "Long long description";
    life.owner = cat;
    s.persist(life);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    Criteria crit = s.createCriteria(Life.class);
    crit.createCriteria("owner").add(Restrictions.eq("name", "kitty"));
    life = (Life) crit.uniqueResult();
    assertEquals("Long long description", life.fullDescription);
    s.delete(life.owner);
    s.delete(life);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 20 with Criteria

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

the class ImmutableNaturalKeyLookupTest method getCriteria.

private Criteria getCriteria(Session s) {
    Criteria crit = s.createCriteria(A.class, "anAlias");
    crit.add(Restrictions.naturalId().set("name", "name1"));
    crit.setFlushMode(FlushMode.COMMIT);
    crit.setCacheable(true);
    return crit;
}
Also used : Criteria(org.hibernate.Criteria)

Aggregations

Criteria (org.hibernate.Criteria)174 Session (org.hibernate.Session)92 Test (org.junit.Test)69 Transaction (org.hibernate.Transaction)39 List (java.util.List)35 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)4 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)4 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Example (org.hibernate.criterion.Example)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2