Examples of OpenJPAQuery


Examples of org.apache.openjpa.persistence.OpenJPAQuery

    public void dfgFieldsLoadedHelper(boolean related) {
        OpenJPAEntityManager em = (OpenJPAEntityManager) factory
            .createEntityManager();
        startTx(em);
        OpenJPAQuery q;
        Collection c;
        try {

            q = em.createQuery(
                "select a FROM " + CacheObjectA.class.getSimpleName()
                    + " a where a.name = :pName").setParameter("pName",
                ORIG_NAME);

            c = new ArrayList((Collection) q.getResultList());
            assertEquals(1, c.size());
            CacheObjectA a = (CacheObjectA) c.iterator().next();
            if (related)
                a.getRelatedArray();
            em.detach(a);
            assertEquals(ORIG_NAME, a.getName());
            q.closeAll();
        }
        finally {
            rollbackTx(em);
            endEm(em);
        }
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

        Object oid2 = pm.getObjectId(pc2);
        endEm(pm);

        pm = (OpenJPAEntityManager) _pmf.createEntityManager();

        OpenJPAQuery q = pm.createQuery(
            "select a FROM " + CacheObjectA.class.getSimpleName() +
                " a where a.name = 'pc1'");
        List res = (List) q.getResultList();

        OpenJPAEntityManager pm2 =
            (OpenJPAEntityManager) _pmf.createEntityManager();
        startTx(pm2);
        pc1 = (CacheObjectA) pm2.find(CacheObjectA.class, oid1);
        pc2 = (CacheObjectA) pm2.find(CacheObjectA.class, oid2);
        pc1.setName("pc2");
        pc2.setName("pc1");
        endTx(pm2);

        assertEquals(1, res.size());
        for (Iterator itr = res.iterator(); itr.hasNext();)
            assertEquals(oid1, pm2.getObjectId(itr.next()));
        endEm(pm2);
        endEm(pm);

        pm = (OpenJPAEntityManager) _pmf.createEntityManager();

        q = pm.createQuery(
            "select a FROM " + CacheObjectA.class.getSimpleName() +
                " a where a.name = 'pc1'");
        res = (List) q.getResultList();

        assertEquals(oid2, pm.getObjectId(res.iterator().next()));
        endEm(pm);
    }
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

        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

Examples of org.apache.openjpa.persistence.OpenJPAQuery

        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

Examples of org.apache.openjpa.persistence.OpenJPAQuery

        em = factory.createEntityManager();
        try {
            startTx(em);

            // find the new object...
            OpenJPAQuery q = em.createQuery("select a FROM "
                + CacheObjectE.class.getSimpleName()
                + " a where a.str = 'e'");
            e = (CacheObjectE) ((Collection) q.getResultList()).iterator()
                .next();

            // ... and modify the changed object.
            e.setStr("e2");
            e.setStr("e3");
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

        finally {
            endEm(em);
        }

        em = (OpenJPAEntityManager) factory.createEntityManager();
        OpenJPAQuery q;
        Collection c;
        try {
            q = em.createQuery("select a FROM "
                + CacheObjectE.class.getSimpleName()
                + " a where a.str = 'e'");
            c = new ArrayList((Collection) q.getResultList());
            assertEquals(1, c.size());
            q.closeAll();
        }
        finally {
            endEm(em);
        }

        try {
            em = (OpenJPAEntityManager) factory.createEntityManager();
            q = em.createQuery("select a FROM "
                + CacheObjectE.class.getSimpleName()
                + " a where a.str = 'e'");
            q.setCandidateCollection(new ArrayList(0));
            c = (Collection) q.getResultList();
            assertEquals(0, c.size());
            q.closeAll();
        }
        finally {
            endEm(em);
        }
    }
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

    public void dfgFieldsLoadedHelper(boolean related) {
        OpenJPAEntityManager em = (OpenJPAEntityManager) factory
            .createEntityManager();
        startTx(em);
        OpenJPAQuery q;
        Collection c;
        try {

            q = em.createQuery(
                "select a FROM " + CacheObjectA.class.getSimpleName()
                    + " a where a.name = :pName").setParameter("pName",
                ORIG_NAME);

            c = new ArrayList((Collection) q.getResultList());
            assertEquals(1, c.size());
            CacheObjectA a = (CacheObjectA) c.iterator().next();
            if (related)
                a.getRelatedArray();
            em.detach(a);
            assertEquals(ORIG_NAME, a.getName());
            q.closeAll();
        }
        finally {
            rollbackTx(em);
            endEm(em);
        }
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

                conf.getQueryTimeout());
            // verify no default javax.persistence.query.timeout is supplied
            // as the Map properties are not passed through as hints
            em = emf.createEntityManager();
            assertNotNull(em);
            OpenJPAQuery q = em.createNamedQuery("NoHintSingle");
            Map<String, Object> hints = q.getHints();
            assertFalse(hints.containsKey("javax.persistence.query.timeout"));
            // verify internal config values were updated
            assertEquals("Map provided query timeout", setTime.intValue(),
                q.getFetchPlan().getQueryTimeout());
           
            try {
                long startTime = System.currentTimeMillis();
                Object result = q.getSingleResult();
                long endTime = System.currentTimeMillis();
                long runTime = endTime - startTime;
                getLog().trace("testQueryTimeout21b() - NoHintSingle runTime " +
                    "msecs=" + runTime);
                // Hack - Windows sometimes returns 1999 instead of 2000+
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

            assertEquals("PU provided query timeout", setTime.intValue(),
                conf.getQueryTimeout());
            // create EM and Query
            em = emf.createEntityManager();
            assertNotNull(em);
            OpenJPAQuery q = em.createNativeQuery(nativeUpdateStr);
            q.setParameter(1, new String("updated"));
            // verify no default javax.persistence.query.timeout is supplied
            Map<String, Object> hints = q.getHints();
            assertFalse(hints.containsKey("javax.persistence.query.timeout"));
            // verify internal config values were updated
            assertEquals("PU provided query timeout", setTime.intValue(),
                q.getFetchPlan().getQueryTimeout());

            // verify queryTimeout on EM find operations
            em.getTransaction().begin();
            // if we get a QTE, then retry this once to prove no db rollback
            for (int i=0; i<=1 && bRetry; i++)
            {
                try {
                    long startTime = System.currentTimeMillis();
                    @SuppressWarnings("unused")
                    int count = q.executeUpdate();
                    // exception should occur before commit
                    em.getTransaction().commit();
                    long endTime = System.currentTimeMillis();
                    long runTime = endTime - startTime;
                    getLog().trace("testQueryTimeout31c() - executeUpdate " +
View Full Code Here

Examples of org.apache.openjpa.persistence.OpenJPAQuery

            assertEquals("Map provided query timeout", setTime.intValue(),
                conf.getQueryTimeout());
            // create EM and named query
            em = emf.createEntityManager();
            assertNotNull(em);
            OpenJPAQuery q = em.createNamedQuery("Hint1000msec");
            setTime = 1000;
            // verify javax.persistence.query.timeout hint via annotation set
            Map<String, Object> hints = q.getHints();
            assertTrue(hints.containsKey("javax.persistence.query.timeout"));
            Integer timeout = new Integer((String) hints.get(
                "javax.persistence.query.timeout"));
            getLog().trace(
                "testQueryTimeout32a() - Found javax.persistence.query.timeout="
                + timeout);
            assertTrue("Expected to find a javax.persistence.query.timeout="
                + setTime, (timeout.intValue() == setTime.intValue()));
            // verify internal config values were updated
            assertEquals("QueryHint provided query timeout", setTime.intValue(),
                q.getFetchPlan().getQueryTimeout());

            try {
                long startTime = System.currentTimeMillis();
                @SuppressWarnings( { "unchecked", "unused" })
                List results = q.getResultList();
                long endTime = System.currentTimeMillis();
                long runTime = endTime - startTime;
                getLog().trace(
                    "testQueryTimeout32a() - Hint1000msec runTime msecs="
                    + runTime);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.