Package org.apache.openjpa.persistence

Examples of org.apache.openjpa.persistence.OpenJPAQuery


    }

    public void testProjections()
        throws Exception {
        OpenJPAEntityManager pm =(OpenJPAEntityManager)currentEntityManager();
        OpenJPAQuery q = pm.createNativeQuery("",Relations.class);
        //FIXME jthomas
        /*
        q.setResult("base, baseSub1, baseSub1Sub2");
        q.setUnique(true);
        Object[] res = (Object[]) q.execute();
View Full Code Here


        getLog().trace("testNoProperties() - no properties in persistence.xml");
        OpenJPAEntityManagerFactory emf1 = null, emf2 = null;
        OpenJPAEntityManager em1 = null, em2 = null;
       
        try {
            OpenJPAQuery q;
            Map<String, Object> hints;
            Integer timeout;
            Integer lTime = new Integer(0);
            Integer qTime = new Integer(0);
           
            // create our PU without properties
            emf1 = OpenJPAPersistence.createEntityManagerFactory(
                "qtimeout-no-properties", "persistence3.xml");
            assertNotNull(emf1);
            emf2 = OpenJPAPersistence.createEntityManagerFactory(
                "qtimeout-no-properties", "persistence3.xml", props);
            assertNotNull(emf2);
           
            //=============
            // Test for 1a)
            //=============
            // verify no config properties from persistence.xml
            OpenJPAConfiguration conf1 = emf1.getConfiguration();
            assertNotNull(conf1);
            assertEquals("Expected no default lock timeout", lTime.intValue(),
                conf1.getLockTimeout());
            assertEquals("Expected no default query timeout", qTime.intValue(),
                conf1.getQueryTimeout());
            // verify Query receives no properties
            em1 = emf1.createEntityManager();
            assertNotNull(em1);
            q = em1.createNamedQuery("NoHintList");
            // verify no Query hints
            hints = q.getHints();
            assertFalse(hints.containsKey("javax.persistence.lock.timeout"));
            assertFalse(hints.containsKey("javax.persistence.query.timeout"));
            // verify default config values of no timeouts
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected no default lock timeout", lTime.intValue(),
                timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected no default query timeout", qTime.intValue(),
                timeout.intValue());

            //=============
            // Test for 2a)
            //=============
            // verify properties in Map override persistence.xml
            OpenJPAConfiguration conf2 = emf2.getConfiguration();
            assertNotNull(conf2);
            lTime = 12000;
            qTime = 7000;
            assertEquals("Expected Map updated lock timeout", lTime.intValue(),
                conf2.getLockTimeout());
            assertEquals("Expected Map updated query timeout", qTime.intValue(),
                conf2.getQueryTimeout());
            // Verify Query receives the properties
            em2 = emf2.createEntityManager();
            assertNotNull(em2);
            q = em2.createNamedQuery("NoHintList");
            // Cannot verify properties are passed through as Query hints
            /*
             * Following test would fail, as the code currently does not pass
             * the properties down as hints, but only as config settings.
             *
             * The spec says that PU or Map provided properties to the EMF
             * will be used as defaults and that Query.setHint() can be used
             * to override, but there is no requirement for getHints() to
             * return these default values.
             * 
            hints = q.getHints();
            assertTrue(hints.containsKey("javax.persistence.lock.timeout"));
            assertTrue(hints.containsKey("javax.persistence.query.timeout"));
            timeout = new Integer((String) hints.get(
                "javax.persistence.lock.timeout"));
            assertEquals("Expected Map updated lockTimeout",
                lTime.intValue(), timeout.intValue());
            timeout = new Integer((String) hints.get(
                "javax.persistence.query.timeout"));
            assertEquals("Expected Map updated queryTimeout",
                qTime.intValue(), timeout.intValue());
            */
            // verify internal config values were updated
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected Map updated lock timeout", lTime.intValue(),
                timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected Map updated query timeout", qTime.intValue(),
                timeout.intValue());
           
            //=============
            // Test for 4a)
            //=============
            // verify setHint overrides Map provided properties
            lTime = 15000;
            qTime = 10000;
            q.setHint("javax.persistence.lock.timeout", lTime);
            q.setHint("javax.persistence.query.timeout", qTime);
            hints = q.getHints();
            // verify getHints values were updated
            timeout = (Integer) hints.get("javax.persistence.lock.timeout");
            assertEquals(
                "Expected setHint updated javax.persistence.lock.timeout",
                lTime.intValue(), timeout.intValue());
            timeout = (Integer) hints.get("javax.persistence.query.timeout");
            assertEquals(
                "Expected setHint updated javax.persistence.query.timeout",
                qTime.intValue(), timeout.intValue());
            // verify internal config values were updated
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected setHint updated lockTimeout",
                lTime.intValue(), timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected setHint updated queryTimeout",
                qTime.intValue(), timeout.intValue());
        } finally {
            // cleanup
            closeEMF(emf1);
View Full Code Here

        getLog().trace("testWithProperties() - properties in persistence.xml");
        OpenJPAEntityManagerFactory emf1 = null, emf2 = null;
        OpenJPAEntityManager em1 = null, em2 = null;
       
        try {
            OpenJPAQuery q;
            Map<String, Object> hints;
            Integer timeout;
            Integer lTime = new Integer(10000);
            Integer qTime = new Integer(5000);
           
            // create our PU with properties
            emf1 = OpenJPAPersistence.createEntityManagerFactory(
                "qtimeout-with-properties", "persistence3.xml");
            assertNotNull(emf1);
            emf2 = OpenJPAPersistence.createEntityManagerFactory(
                "qtimeout-with-properties", "persistence3.xml", props);
            assertNotNull(emf2);
           
            //=============
            // Test for 1b)
            //=============
            // verify properties in persistence.xml
            OpenJPAConfiguration conf1 = emf1.getConfiguration();
            assertNotNull(conf1);
            assertEquals("Default PU lock timeout", lTime.intValue(),
                conf1.getLockTimeout());
            assertEquals("Default PU query timeout.", qTime.intValue(),
                conf1.getQueryTimeout());
            // verify Query receives the properties
            em1 = emf1.createEntityManager();
            assertNotNull(em1);
            q = em1.createNamedQuery("NoHintList");   
            // Cannot verify properties are passed through as Query hints
            //     See explanation and commented out test in testNoProperties()
            // verify timeout properties supplied in persistence.xml
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected default PU lock timeout", lTime.intValue(),
                timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected default PU query timeout", qTime.intValue(),
                timeout.intValue());

            //=============
            // Test for 2b)
            //=============
            // verify properties in Map override persistence.xml
            OpenJPAConfiguration conf2 = emf2.getConfiguration();
            assertNotNull(conf2);
            lTime = 12000;
            qTime = 7000;
            assertEquals("Expected Map updated lock timeout", lTime.intValue(),
                conf2.getLockTimeout());
            assertEquals("Expected Map updated query timeout", qTime.intValue(),
                conf2.getQueryTimeout());
            // Verify Query receives the properties
            em2 = emf2.createEntityManager();
            assertNotNull(em2);
            q = em2.createNamedQuery("NoHintList");
            // Cannot verify properties are passed through as Query hints
            //     See explanation and commented out test in testNoProperties()
            // verify internal config values were updated
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected Map updated lockTimeout", lTime.intValue(),
                timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected Map updated queryTimeout", qTime.intValue(),
                timeout.intValue());
           
            //=============
            // Test for 3)
            //=============
            // verify QueryHints override Map provided properties
            q = em2.createNamedQuery("Hint1000msec");
            qTime = 1000;
            // verify getHints values were updated
            hints = q.getHints();
            timeout = new Integer((String)hints.get(
                    "javax.persistence.query.timeout"));
            assertEquals("Expected QueryHints updated query timeout",
                qTime.intValue(), timeout.intValue());
            // verify internal config value was updated
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected QueryHints updated queryTimeout",
                qTime.intValue(), timeout.intValue());

            //=============
            // Test for 4b)
            //=============
            // verify setHint overrides QueryHint provided properties
            lTime = 15000;
            qTime = 10000;
            q.setHint("javax.persistence.lock.timeout", lTime);
            q.setHint("javax.persistence.query.timeout", qTime);
            // verify getHints values were updated
            hints = q.getHints();
            timeout = (Integer) hints.get("javax.persistence.lock.timeout");
            assertEquals("Expected setHint updated lock timeout",
                lTime.intValue(), timeout.intValue());
            timeout = (Integer) hints.get("javax.persistence.query.timeout");
            assertEquals("Expected setHint updated query timeout",
                qTime.intValue(), timeout.intValue());
            // verify internal config values were updated
            timeout = q.getFetchPlan().getLockTimeout();
            assertEquals("Expected setHint updated lockTimeout",
                lTime.intValue(), timeout.intValue());
            timeout = q.getFetchPlan().getQueryTimeout();
            assertEquals("Expected setHint updated queryTimeout",
                qTime.intValue(), timeout.intValue());
        } finally {
            // cleanup
            closeEMF(emf1);
View Full Code Here

        OpenJPAEntityManager pm1 = getEm(true, true);
        OpenJPAEntityManager pm2 = getEm(true, true);
       
        // have to load via query or extent where we're selecting the vertical
        // field in the initial SELECT
        OpenJPAQuery q1 = pm1.createNativeQuery("",StateImagePC2.class);
        //FIXME  jthomas
        //q1.setOrdering("intField ascending");
        StateImagePC2 pc1 =
            (StateImagePC2) ((Collection) q1.getCandidateCollection()).
            iterator().next();
        q1.closeAll();
       
        OpenJPAQuery q2 = pm2.createNativeQuery("",StateImagePC2.class);
        //FIXME jthomas
        //q2.setOrdering("intField ascending");
        StateImagePC2 pc2 =
            (StateImagePC2) ((Collection) q2.getCandidateCollection()).
            iterator().next();
        q2.closeAll();
       
        pm1.getTransaction().begin();
        pc1.setStringField("changed1");
        pc1.setStateImage(null);
       
View Full Code Here

        EntityManager em =currentEntityManager();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);

        String theQuery = "select a FROM " + PagingPC.class.getSimpleName()
            + " a where a.intField >= 0";
        OpenJPAQuery q = kem.createQuery(theQuery);


    //FIXME jthomas commenting till we find how ordering can be done
        //q.setOrdering("intField ascending");
        q.getFetchPlan().setFetchBatchSize(2);    // 2 pages of 2
        q.getFetchPlan().addFetchGroup("rel");
        q.getFetchPlan().addFetchGroup("rels");

        // we should get four selects: the initial select, the count, and the
        // two IN eager stmnts
        List list = (List) q.getResultList();
        assertEquals(list.toString(), 4, list.size());
        // so we don't have to re-execute to move back to beginning
        list = new ArrayList(list);
        assertRel(list, 4);
        assertRels(list, 4);

        assertEquals(sql.toString(), 4, sql.size());
        assertTrue((String) sql.get(1),
            matches(" COUNT", (String) sql.get(1)));
        assertTrue((String) sql.get(2),
            matches(" IN ", (String) sql.get(2)));
        assertTrue((String) sql.get(3),
            matches(" IN ", (String) sql.get(3)));
        sql.clear();

        // if we access a result object on the second page, we should get only
        // three selects: the initial select, the count, and the IN eager stmnt
        // for page 2
        list = (List) q.getResultList();
        assertEquals(list.toString(), 4, list.size());
        PagingPC pc = (PagingPC) list.get(2);
        assertEquals(2, pc.getIntField());
        assertEquals(sql.toString(), 3, sql.size());
        assertTrue((String) sql.get(2),
View Full Code Here

        EntityManager em =currentEntityManager();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);

        String theQuery = "select a FROM " + PagingPC.class.getSimpleName()
            + " a where a.intField >= 0";
        OpenJPAQuery q = kem.createQuery(theQuery);


        //FIXME jthomas commenting till we find how ordering can be done
        //q.setOrdering("intField ascending");
        q.getFetchPlan().setFetchBatchSize(3);    // 1 on page 2
        q.getFetchPlan().addFetchGroups("rel");
        q.getFetchPlan().addFetchGroups("rels");

        // if we access a result object on the second page, we should get only
        // two selects: the initial select and the eager stmnt for page
        List list = (List) q.getResultList();
        PagingPC pc = (PagingPC) list.get(3);
        assertEquals(3, pc.getIntField());
        assertEquals(list.toString(), 4, list.size());
        assertEquals(sql.toString(), 2, sql.size());
        // for single result, should use standard select, not IN
View Full Code Here

        super(test);
    }

    public void testQuery() {
        OpenJPAEntityManager pm =(OpenJPAEntityManager)currentEntityManager();
        OpenJPAQuery q = pm.createNativeQuery("",NoClassColumn.class);
        //FIXME jthomas
        //q.declareParameters("java.lang.String input");
        //q.setFilter("test==input");
        Map params = new HashMap();
        params.put("input", "blah");
View Full Code Here

        String theQuery = "select a FROM " + PagingPC.class.getSimpleName()
            + " a where a.intField >= 0";

        //OpenJPAQuery q = kem.createQuery(PagingPC.class.getSimpleName(),
    //    "intField >= 0");
        OpenJPAQuery q = kem.createQuery(theQuery);

        q.getFetchPlan().setFetchBatchSize(0);
        q.getFetchPlan().addFetchGroups("rel");

        // if we only access the rel field, we should only have used two
        // selects: one for the query and one for the size
        List list = (List) q.getResultList();
        assertEquals(4, list.size());
        assertRel(list, 4);
        assertEquals(sql.toString(), 2, sql.size());
        assertTrue((String) sql.get(0),
            matches(" >= ", (String) sql.get(0)));
View Full Code Here

        EntityManager em =currentEntityManager();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast (em);

        String theQuery = "select a FROM " + PagingPC.class.getSimpleName()
            + " a where a.intField >= 0";
        OpenJPAQuery q = kem.createQuery(theQuery);
        q.getFetchPlan().setFetchBatchSize(0);
        q.getFetchPlan().addFetchGroups("rel");
        q.getFetchPlan().addFetchGroups("rels");


        // we should get three selects: the initial select, the COUNT for the
        // size, and the IN eager stmnt
        List list = (List) q.getResultList();
        assertEquals(4, list.size());
        assertRel(list, 4);
        assertRels(list, 4);
        assertEquals(sql.toString(), 3, sql.size());
        assertTrue((String) sql.get(0),
            matches(" >= ", (String) sql.get(0)));
        assertTrue((String) sql.get(1),
            matches(" COUNT", (String) sql.get(1)));
        assertTrue((String) sql.get(2),
            matches(" IN ", (String) sql.get(2)));
        assertFalse((String) sql.get(2),
            matches(" >= ", (String) sql.get(2)));
        sql.clear();

        // if we execute and traverse a result before getting the size, we
        // should only get 2 selects, since the caching of the page should
        // reach the end of the list and therefore know the total size
        list = (List) q.getResultList();
        list.get(0);
        assertEquals(4, list.size());
        assertRel(list, 4);
        assertRels(list, 4);
        assertEquals(sql.toString(), 2, sql.size());
View Full Code Here

        EntityManager em =currentEntityManager();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);

        String theQuery = "select a FROM " + PagingAppIdPC.class.getSimpleName()
            + " a where a.intField >= 0";
        OpenJPAQuery q = kem.createQuery(theQuery);


        q.getFetchPlan().setFetchBatchSize(0);
        q.getFetchPlan().addFetchGroups("rel");
        q.getFetchPlan().addFetchGroups("rels");

        // we should get three selects: the initial select, the COUNT for the
        // size, and the OR eager stmnt
        List list = (List) q.getResultList();
        assertEquals(4, list.size());
        assertAppIdRel(list, 4);
        assertAppIdRels(list, 4);
        assertEquals(sql.toString(), 3, sql.size());
        assertTrue((String) sql.get(0),
View Full Code Here

TOP

Related Classes of org.apache.openjpa.persistence.OpenJPAQuery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.