Search in sources :

Example 6 with DetachedCriteria

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

the class AssetRecordDaoHibernate method getDistinctProperties.

@Override
public List<OnmsAssetRecord> getDistinctProperties() {
    DetachedCriteria criteria = DetachedCriteria.forClass(OnmsAssetRecord.class);
    ProjectionList projList = Projections.projectionList();
    // projList.add(Projections.alias(Projections.property("geolocation"), "geolocation"));
    projList.add(Projections.alias(Projections.property("additionalhardware"), "additionalhardware"));
    projList.add(Projections.alias(Projections.property("geolocation.address1"), "address1"));
    projList.add(Projections.alias(Projections.property("geolocation.address2"), "address2"));
    projList.add(Projections.alias(Projections.property("admin"), "admin"));
    projList.add(Projections.alias(Projections.property("assetNumber"), "assetNumber"));
    projList.add(Projections.alias(Projections.property("autoenable"), "autoenable"));
    projList.add(Projections.alias(Projections.property("building"), "building"));
    projList.add(Projections.alias(Projections.property("category"), "category"));
    projList.add(Projections.alias(Projections.property("circuitId"), "circuitId"));
    projList.add(Projections.alias(Projections.property("geolocation.city"), "city"));
    projList.add(Projections.alias(Projections.property("comment"), "comment"));
    projList.add(Projections.alias(Projections.property("connection"), "connection"));
    projList.add(Projections.alias(Projections.property("geolocation.longitude"), "longitude"));
    projList.add(Projections.alias(Projections.property("geolocation.latitude"), "latitude"));
    projList.add(Projections.alias(Projections.property("cpu"), "cpu"));
    projList.add(Projections.alias(Projections.property("department"), "department"));
    projList.add(Projections.alias(Projections.property("description"), "description"));
    projList.add(Projections.alias(Projections.property("displayCategory"), "displayCategory"));
    projList.add(Projections.alias(Projections.property("division"), "division"));
    projList.add(Projections.alias(Projections.property("enable"), "enable"));
    projList.add(Projections.alias(Projections.property("floor"), "floor"));
    projList.add(Projections.alias(Projections.property("hdd1"), "hdd1"));
    projList.add(Projections.alias(Projections.property("hdd2"), "hdd2"));
    projList.add(Projections.alias(Projections.property("hdd3"), "hdd3"));
    projList.add(Projections.alias(Projections.property("hdd4"), "hdd4"));
    projList.add(Projections.alias(Projections.property("hdd5"), "hdd5"));
    projList.add(Projections.alias(Projections.property("hdd6"), "hdd6"));
    projList.add(Projections.alias(Projections.property("inputpower"), "inputpower"));
    projList.add(Projections.alias(Projections.property("lease"), "lease"));
    projList.add(Projections.alias(Projections.property("maintcontract"), "maintcontract"));
    projList.add(Projections.alias(Projections.property("manufacturer"), "manufacturer"));
    projList.add(Projections.alias(Projections.property("modelNumber"), "modelNumber"));
    projList.add(Projections.alias(Projections.property("notifyCategory"), "notifyCategory"));
    projList.add(Projections.alias(Projections.property("numpowersupplies"), "numpowersupplies"));
    projList.add(Projections.alias(Projections.property("operatingSystem"), "operatingSystem"));
    projList.add(Projections.alias(Projections.property("pollerCategory"), "pollerCategory"));
    projList.add(Projections.alias(Projections.property("port"), "port"));
    projList.add(Projections.alias(Projections.property("rack"), "rack"));
    projList.add(Projections.alias(Projections.property("ram"), "ram"));
    projList.add(Projections.alias(Projections.property("region"), "region"));
    projList.add(Projections.alias(Projections.property("room"), "room"));
    projList.add(Projections.alias(Projections.property("serialNumber"), "serialNumber"));
    projList.add(Projections.alias(Projections.property("slot"), "slot"));
    projList.add(Projections.alias(Projections.property("snmpcommunity"), "snmpcommunity"));
    projList.add(Projections.alias(Projections.property("geolocation.state"), "state"));
    projList.add(Projections.alias(Projections.property("storagectrl"), "storagectrl"));
    projList.add(Projections.alias(Projections.property("supportPhone"), "supportPhone"));
    projList.add(Projections.alias(Projections.property("thresholdCategory"), "thresholdCategory"));
    projList.add(Projections.alias(Projections.property("username"), "username"));
    projList.add(Projections.alias(Projections.property("vendor"), "vendor"));
    projList.add(Projections.alias(Projections.property("vendorAssetNumber"), "vendorAssetNumber"));
    projList.add(Projections.alias(Projections.property("vendorFax"), "vendorFax"));
    projList.add(Projections.alias(Projections.property("vendorPhone"), "vendorPhone"));
    projList.add(Projections.alias(Projections.property("geolocation.zip"), "zip"));
    projList.add(Projections.alias(Projections.property("vmwareManagedObjectId"), "vmwareManagedObjectId"));
    projList.add(Projections.alias(Projections.property("vmwareManagedEntityType"), "vmwareManagedEntityType"));
    projList.add(Projections.alias(Projections.property("vmwareManagementServer"), "vmwareManagementServer"));
    projList.add(Projections.alias(Projections.property("vmwareTopologyInfo"), "vmwareTopologyInfo"));
    projList.add(Projections.alias(Projections.property("vmwareState"), "vmwareState"));
    criteria.setProjection(Projections.distinct(projList));
    criteria.setResultTransformer(Transformers.aliasToBean(OnmsAssetRecord.class));
    @SuppressWarnings("unchecked") List<OnmsAssetRecord> result = (List<OnmsAssetRecord>) getHibernateTemplate().findByCriteria(criteria);
    return result;
}
Also used : OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) List(java.util.List) ProjectionList(org.hibernate.criterion.ProjectionList) ProjectionList(org.hibernate.criterion.ProjectionList)

Example 7 with DetachedCriteria

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

the class CriteriaQueryTest method testSubselectWithComponent.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselectWithComponent() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    CityState odessaWa = new CityState("Odessa", "WA");
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setCityState(odessaWa);
    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("cityState").eq(odessaWa)).setProjection(Property.forName("cityState"));
    session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
    t.commit();
    session.close();
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.propertyEqAll("cityState", dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Property.forName("cityState").eqAll(dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.in(odessaWa, dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st1").add(Property.forName("st1.cityState").eqProperty("st2.cityState")).setProjection(Property.forName("cityState"));
    try {
        session.createCriteria(Student.class, "st2").add(Subqueries.eq(odessaWa, dc2)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.cityState"));
    try {
        session.createCriteria(Enrolment.class, "e").add(Subqueries.eq(odessaWa, dc3)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    session.delete(enrolment2);
    session.delete(gavin);
    session.delete(course);
    t.commit();
    session.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) JDBCException(org.hibernate.JDBCException) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 8 with DetachedCriteria

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

the class CriteriaQueryTest method testDetachedCriteriaTimeout.

@Test
public void testDetachedCriteriaTimeout() {
    doInHibernate(this::sessionFactory, session -> {
        DetachedCriteria dc = DetachedCriteria.forClass(Student.class).setTimeout(100);
        Student gavin = new Student();
        gavin.setName("Gavin King");
        gavin.setStudentNumber(232);
        session.persist(gavin);
        session.flush();
        connectionProvider.clear();
        List result = dc.getExecutableCriteria(session).list();
        PreparedStatement preparedStatement = connectionProvider.getPreparedStatements().get(0);
        try {
            verify(preparedStatement, times(1)).setQueryTimeout(100);
        } catch (SQLException e) {
            fail(e.getMessage());
        }
        assertEquals(result.size(), 1);
        session.delete(gavin);
    });
}
Also used : SQLException(java.sql.SQLException) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) List(java.util.List) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 9 with DetachedCriteria

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

the class CriteriaQueryTest method testNestedCorrelatedSubquery.

@Test
@TestForIssue(jiraKey = "HHH-7194")
public void testNestedCorrelatedSubquery() throws Exception {
    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);
    gavin.setPreferredCourse(course);
    Enrolment enrolment = new Enrolment();
    enrolment.setCourse(course);
    enrolment.setCourseCode(course.getCourseCode());
    enrolment.setSemester((short) 1);
    enrolment.setYear((short) 1999);
    enrolment.setStudent(gavin);
    enrolment.setStudentNumber(gavin.getStudentNumber());
    session.persist(course);
    session.persist(gavin);
    session.persist(enrolment);
    session.flush();
    session.clear();
    //execute a nested subquery
    DetachedCriteria mainCriteria = DetachedCriteria.forClass(Student.class, "student");
    DetachedCriteria nestedSubQuery = DetachedCriteria.forClass(Enrolment.class, "maxStudentEnrolment");
    nestedSubQuery.add(Restrictions.eqProperty("student.preferredCourse", "maxStudentEnrolment.course"));
    nestedSubQuery.setProjection(Projections.max("maxStudentEnrolment.year"));
    DetachedCriteria subQuery = DetachedCriteria.forClass(Enrolment.class, "enrolment");
    subQuery.add(Subqueries.propertyEq("enrolment.year", nestedSubQuery));
    subQuery.setProjection(Projections.property("student"));
    mainCriteria.add(Subqueries.exists(subQuery));
    //query should complete and return gavin in the list
    List results = mainCriteria.getExecutableCriteria(session).list();
    assertEquals(1, results.size());
    assertEquals(gavin.getStudentNumber(), ((Student) results.get(0)).getStudentNumber());
    t.rollback();
    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 10 with DetachedCriteria

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

the class CriteriaQueryWithAppliedFilterTest method testSubqueryWithRestrictionsOnComponentTypes2.

@Test
public void testSubqueryWithRestrictionsOnComponentTypes2() {
    doInHibernate(this::sessionFactory, session -> {
        session.enableFilter("statusFilter").setParameter("status", "active");
        final Criteria query = session.createCriteria(Student.class);
        query.add(Restrictions.eq("id", STUDENT_ID));
        final DetachedCriteria subSelect = DetachedCriteria.forClass(Student.class);
        subSelect.setProjection(Projections.max("age"));
        subSelect.add(Restrictions.eq("address", new Address("London", "Lollard St")));
        subSelect.add(Restrictions.eq("id", STUDENT_ID));
        query.add(Property.forName("age").eq(subSelect));
        final List list = query.list();
        assertThat(list.size(), is(1));
    });
}
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