Search in sources :

Example 1 with DetachedCriteria

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

the class CriteriaQueryTest method testMultiplePropertiesSubquery.

@Test
@TestForIssue(jiraKey = "HHH-6766")
public void testMultiplePropertiesSubquery() {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    Man lukasz = new Man();
    lukasz.setName("Lukasz");
    lukasz.setHeight(170);
    lukasz.setWeight(60);
    session.persist(lukasz);
    Man robert = new Man();
    robert.setName("Robert");
    robert.setHeight(170);
    robert.setWeight(78);
    session.persist(robert);
    Woman kinga = new Woman();
    kinga.setName("Kinga");
    kinga.setHeight(170);
    kinga.setWeight(60);
    session.persist(kinga);
    tx.commit();
    session.close();
    session = openSession();
    tx = session.beginTransaction();
    DetachedCriteria sizeQuery = DetachedCriteria.forClass(Man.class).setProjection(Projections.projectionList().add(Projections.property("weight")).add(Projections.property("height"))).add(Restrictions.eq("name", "Lukasz"));
    List result;
    if (getDialect().supportsRowValueConstructorSyntax()) {
        result = session.createCriteria(Woman.class).add(Subqueries.propertiesEq(new String[] { "weight", "height" }, sizeQuery)).list();
        assertEquals(1, result.size());
        assertEquals(kinga, result.get(0));
    }
    if (getDialect().supportsRowValueConstructorSyntaxInInList()) {
        result = session.createCriteria(Woman.class).add(Subqueries.propertiesIn(new String[] { "weight", "height" }, sizeQuery)).list();
        assertEquals(1, result.size());
        assertEquals(kinga, result.get(0));
    }
    tx.commit();
    session.close();
    session = openSession();
    tx = session.beginTransaction();
    sizeQuery = DetachedCriteria.forClass(Man.class).setProjection(Projections.projectionList().add(Projections.property("weight")).add(Projections.property("height"))).add(Restrictions.ne("name", "Lukasz"));
    if (getDialect().supportsRowValueConstructorSyntax()) {
        result = session.createCriteria(Woman.class).add(Subqueries.propertiesNotEq(new String[] { "weight", "height" }, sizeQuery)).list();
        assertEquals(1, result.size());
        assertEquals(kinga, result.get(0));
    }
    if (getDialect().supportsRowValueConstructorSyntaxInInList()) {
        result = session.createCriteria(Woman.class).add(Subqueries.propertiesNotIn(new String[] { "weight", "height" }, sizeQuery)).list();
        assertEquals(1, result.size());
        assertEquals(kinga, result.get(0));
    }
    tx.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with DetachedCriteria

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

the class CriteriaQueryTest method testDetachedCriteria.

@Test
public void testDetachedCriteria() {
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("name").eq("Gavin King")).addOrder(Order.asc("studentNumber")).setProjection(Property.forName("studentNumber"));
    byte[] bytes = SerializationHelper.serialize(dc);
    dc = (DetachedCriteria) SerializationHelper.deserialize(bytes);
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    Student bizarroGavin = new Student();
    bizarroGavin.setName("Gavin King");
    bizarroGavin.setStudentNumber(666);
    session.persist(bizarroGavin);
    session.persist(gavin);
    List result = dc.getExecutableCriteria(session).setMaxResults(3).list();
    assertEquals(result.size(), 2);
    assertEquals(result.get(0), new Long(232));
    assertEquals(result.get(1), new Long(666));
    session.delete(gavin);
    session.delete(bizarroGavin);
    t.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with DetachedCriteria

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

the class CriteriaQueryTest method testSubselect.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselect() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    session.persist(gavin);
    Enrolment enrolment2 = new Enrolment();
    enrolment2.setCourse(course);
    enrolment2.setCourseCode(course.getCourseCode());
    enrolment2.setSemester((short) 3);
    enrolment2.setYear((short) 1998);
    enrolment2.setStudent(gavin);
    enrolment2.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment2);
    session.persist(enrolment2);
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("studentNumber").eq(new Long(232))).setProjection(Property.forName("name"));
    session.createCriteria(Student.class).add(Subqueries.propertyEqAll("name", dc)).list();
    session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
    session.createCriteria(Student.class).add(Property.forName("name").eqAll(dc)).list();
    session.createCriteria(Student.class).add(Subqueries.in("Gavin King", dc)).list();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st").add(Property.forName("st.studentNumber").eqProperty("e.studentNumber")).setProjection(Property.forName("name"));
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc2)).list();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.name"));
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc3)).list();
    DetachedCriteria dc4 = DetachedCriteria.forClass(Student.class, "st").setProjection(Property.forName("name").as("stname"));
    dc4.getExecutableCriteria(session).list();
    // SQL Server and Oracle doesn't normally support ORDER BY in subqueries...
    if (!(getDialect() instanceof SQLServerDialect) && !(getDialect() instanceof Oracle8iDialect)) {
        dc4.getExecutableCriteria(session).addOrder(Order.asc("stname")).list();
    }
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc4)).list();
    session.delete(enrolment2);
    session.delete(gavin);
    session.delete(course);
    t.commit();
    session.close();
}
Also used : SQLServerDialect(org.hibernate.dialect.SQLServerDialect) Transaction(org.hibernate.Transaction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 4 with DetachedCriteria

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

the class DynamicFilterTest method testCriteriaControl.

@Test
public void testCriteriaControl() {
    TestData testData = new TestData();
    testData.prepare();
    // the subquery...
    DetachedCriteria subquery = DetachedCriteria.forClass(Salesperson.class).setProjection(Property.forName("name"));
    Session session = openSession();
    session.beginTransaction();
    session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
    session.enableFilter("regionlist").setParameterList("regions", new String[] { "APAC" });
    List result = session.createCriteria(Order.class).add(Subqueries.in("steve", subquery)).list();
    assertEquals(1, result.size());
    session.getTransaction().commit();
    session.close();
    testData.release();
}
Also used : DetachedCriteria(org.hibernate.criterion.DetachedCriteria) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with DetachedCriteria

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

the class CriteriaQueryWithAppliedFilterTest method testSubquery.

@Test
public void testSubquery() {
    doInHibernate(this::sessionFactory, session -> {
        final Criteria query = session.createCriteria(Student.class);
        query.add(Restrictions.eq("name", "dre"));
        final DetachedCriteria inner = DetachedCriteria.forClass(Student.class);
        inner.setProjection(Projections.min("age"));
        query.add(Property.forName("age").eq(inner));
        query.add(Restrictions.eq("name", "dre"));
        final List list = query.list();
        assertThat(list.size(), is(1));
    });
    doInHibernate(this::sessionFactory, session -> {
        session.enableFilter("statusFilter").setParameter("status", "deleted");
        final Criteria query = session.createCriteria(Student.class);
        query.add(Restrictions.eq("name", "dre"));
        final DetachedCriteria inner = DetachedCriteria.forClass(Student.class);
        inner.setProjection(Projections.min("age"));
        query.add(Property.forName("age").eq(inner));
        query.add(Restrictions.eq("name", "dre"));
        final List list = query.list();
        assertThat(list.size(), is(0));
    });
}
Also used : DetachedCriteria(org.hibernate.criterion.DetachedCriteria) List(java.util.List) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Test(org.junit.Test)

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