Package org.apache.openjpa.persistence

Examples of org.apache.openjpa.persistence.OpenJPAEntityManagerSPI


    public void testOptimizeForClauseViaQueryHint() {
        testOptimizeForClause(false,true,true);
    }
    public void testOptimizeForClause(boolean hint,
        boolean find, boolean queryHint) {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
            .getDBDictionaryInstance();

        // hsql doesn't support optimizing; circumvent the test
        if (dict instanceof HSQLDictionary)
            return;

        sql.clear();
        try {
            em.getTransaction().begin();
            if (hint || queryHint) {
                if (hint) {
                    Query q = em.createQuery(
                    "select o from AllFieldTypes o where o.intField = :p");
                    q.setParameter("p", 0);
                    q.setHint("openjpa.hint.OptimizeResultCount"
                         ,new Integer(8));
                    q.getResultList();
                }   
                else {
                    OpenJPAQuery q =  OpenJPAPersistence.cast (em.createQuery
                        ("select o from AllFieldTypes o where o.intField " +
                         "= :p"));
                    q.setParameter("p", 0);
                    q.setHint(q.HINT_RESULT_COUNT, new Integer(8));
                    q.getResultList();
               }    
               if (dict instanceof DB2Dictionary) {
                   assertEquals(1, sql.size());
                   assertSQL("SELECT t0.id, t0.booleanField, t0.byteField," +
                       " t0.charField, t0.dateField, t0.doubleField, " +
                       "t0.floatField, t0.intField, t0.longField, " +
                       "t0.shortField, t0.stringField FROM AllFieldTypes " +
                       "t0 WHERE \\(t0.intField = \\?\\)  optimize for 8 row");
               }
            }
            else {
                 em.find(AllFieldTypes.class, 0);
                 if (dict instanceof DB2Dictionary ) {
                    assertEquals(1, sql.size());
                    assertSQL("SELECT t0.booleanField, t0.byteField, " +
                        "t0.charField, t0.dateField, t0.doubleField, " +
                        "t0.floatField, t0.intField, t0.longField, " +
                        "t0.shortField, t0.stringField FROM AllFieldTypes" +
                        " t0 WHERE t0.id = \\?  optimize for 1 row");
                }
                  
            }
        } catch (InvalidStateException pe) {
            // if we're not using DB2, we expect an InvalidStateException.
            if (dict instanceof DB2Dictionary)
                throw pe;
        } finally {
            em.getTransaction().rollback();
            em.close();
        }
    }
View Full Code Here


            "openjpa.LockManager", "pessimistic",
            "openjpa.ReadLockLevel", "none");
    }

    public void testSelectForUpdateOverride() {
        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI)
            OpenJPAPersistence.cast(emf.createEntityManager());
        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
            .getDBDictionaryInstance();

        // hsql doesn't support locking; circumvent the test
        if (dict instanceof HSQLDictionary)
            return;

        sql.clear();
        try {
            em.getTransaction().begin();
            OpenJPAPersistence.cast(em).getFetchPlan()
                .setReadLockMode(LockModeType.WRITE);
            em.find(AllFieldTypes.class, 0);
            assertEquals(1, sql.size());
            if (dict instanceof DB2Dictionary) {
                if ((((DB2Dictionary) dict).getDb2ServerType() == 1)
                    || (((DB2Dictionary) dict).getDb2ServerType()== 2)) {
                    assertEquals(1, sql.size());
                    assertSQL("SELECT t0.booleanField, t0.byteField, "
                        + "t0.charField, t0.dateField, t0.doubleField,"
                        + " t0.floatField, t0.intField, t0.longField,"
                        + " t0.shortField, t0.stringField FROM "
                        + "AllFieldTypes t0 WHERE t0.id = \\? "
                        + " FOR UPDATE OF optimize for 1 row");
                }
                // it is DB2 v82 or later
                else if ((((DB2Dictionary) dict).getDb2ServerType() == 3)
                    || (((DB2Dictionary) dict).getDb2ServerType() == 4)) {
                    assertEquals(1, sql.size());
                    assertSQL("SELECT t0.booleanField, t0.byteField, "
                        + "t0.charField, t0.dateField, t0.doubleField,"
                        + " t0.floatField, t0.intField, t0.longField,"
                        + " t0.shortField, t0.stringField FROM "
                        + "AllFieldTypes t0 WHERE t0.id = \\? "
                        + " FOR READ ONLY WITH RS USE AND KEEP UPDATE LOCKS"
                        + " optimize for 1 row");
                }
                else if (((DB2Dictionary) dict).getDb2ServerType() == 5) {
                    assertEquals(1, sql.size());
                    assertSQL("SELECT t0.booleanField, t0.byteField, "
                        + "t0.charField, t0.dateField, t0.doubleField,"
                        + " t0.floatField, t0.intField, t0.longField,"
                        + " t0.shortField, t0.stringField FROM "
                        + "AllFieldTypes t0 WHERE t0.id = \\? "
                        + " FOR READ ONLY WITH RS USE AND KEEP EXCLUSIVE LOCKS"
                        + " optimize for 1 row");
                }   
                else {
                    fail("OpenJPA currently only supports per-query isolation "
                        + "level configuration on the following databases: "
                        + "DB2");
                }
            }   
        } finally {
            em.getTransaction().rollback();
            em.close();
        }
    }
View Full Code Here

        testIsolationLevelOverride(true, true);
    }

    public void testIsolationLevelOverride(boolean useHintsAndQueries,
        boolean useStringHints) {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
            .getDBDictionaryInstance();

        // hsql doesn't support locking; circumvent the test
        if (dict instanceof HSQLDictionary)
            return;

        sql.clear();
        try {
            em.getTransaction().begin();
            if (useHintsAndQueries) {
                Query q = em.createQuery(
                "select o from AllFieldTypes o where o.intField = :p");
                q.setParameter("p", 0);
                if (useStringHints) {
                    q.setHint("openjpa.FetchPlan.Isolation", "SERIALIZABLE");
                } else {
                    q.setHint("openjpa.FetchPlan.Isolation",
                        IsolationLevel.SERIALIZABLE);
                }

                assertEquals(IsolationLevel.SERIALIZABLE,
                    ((JDBCFetchPlan) ((OpenJPAQuery) q).getFetchPlan())
                        .getIsolation());

                q.getResultList();
                if (dict instanceof DB2Dictionary) {
                    if ((((DB2Dictionary) dict).getDb2ServerType() == 1)
                        || (((DB2Dictionary) dict).getDb2ServerType()== 2)) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.id, t0.booleanField, t0.byteField,"
                            + " t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField, "
                            + "t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE \\(t0.intField = \\?\\) "
                            + " FOR UPDATE OF");
                    }
                    // it is DB2 v82 or later
                    else if ((((DB2Dictionary) dict).getDb2ServerType() == 3)
                        || (((DB2Dictionary) dict).getDb2ServerType() == 4)) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.id, t0.booleanField, t0.byteField,"
                            + " t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField, "
                            + "t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE \\(t0.intField = \\?\\) "
                            + " FOR READ ONLY WITH RR USE AND KEEP "
                            + "UPDATE LOCKS");
                    }
                    else if (((DB2Dictionary) dict).getDb2ServerType() == 5) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.id, t0.booleanField, t0.byteField,"
                            + " t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField, "
                            + "t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE \\(t0.intField = \\?\\) "
                            + " FOR READ ONLY WITH RR USE AND KEEP EXCLUSIVE "
                            + "LOCKS");
                    }   
                    else {
                        fail("OpenJPA currently only supports "
                            +"per-query isolation level configuration on the"
                            +" following databases: DB2");
                    }
                }   
            } else {
                ((JDBCFetchPlan) em.getFetchPlan())
                    .setIsolation(IsolationLevel.SERIALIZABLE);
                em.find(AllFieldTypes.class, 0);
                if (dict instanceof DB2Dictionary ) {
                    if ((((DB2Dictionary) dict).getDb2ServerType() == 1)
                        || (((DB2Dictionary) dict).getDb2ServerType()== 2)) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.booleanField, t0.byteField, "
                            + "t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField,"
                            + " t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE t0.id = \\? "
                            + " FOR UPDATE OF optimize for 1 row");
                    }
                    // it is DB2 v82 or later
                    else if ((((DB2Dictionary) dict).getDb2ServerType() == 3)
                        || (((DB2Dictionary) dict).getDb2ServerType() == 4)) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.booleanField, t0.byteField, "
                            + "t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField,"
                            + " t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE t0.id = \\? "
                            + " FOR READ ONLY WITH RR USE AND KEEP UPDATE LOCKS"
                            + " optimize for 1 row");
                    }
                    else if (((DB2Dictionary) dict).getDb2ServerType() == 5) {
                        assertEquals(1, sql.size());
                        assertSQL("SELECT t0.booleanField, t0.byteField, "
                            + "t0.charField, t0.dateField, t0.doubleField,"
                            + " t0.floatField, t0.intField, t0.longField,"
                            + " t0.shortField, t0.stringField FROM "
                            + "AllFieldTypes t0 WHERE t0.id = \\? "
                            + " FOR READ ONLY WITH RR USE AND KEEP EXCLUSIVE"
                            + " LOCKS optimize for 1 row");
                    }   
                    else {
                        fail("OpenJPA currently only supports per-query"
                            +" isolation level configuration on the following"
                            +" databases: DB2");
                    }
                }   
            }
        } catch (InvalidStateException pe) {
            // if we're not using DB2, we expect an InvalidStateException.
            if (dict instanceof DB2Dictionary)
                throw pe;
        } finally {
            em.getTransaction().rollback();
            em.close();
        }
    }
View Full Code Here

            JPAFacadeHelper.toOpenJPAObjectId(cmd, "a");
            fail("Didn't fail!");
        } catch (UserException re) {
            // expected
        }
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        em.getTransaction().begin();
        AllFieldTypes type = new AllFieldTypes();
        em.persist(type);
        em.getTransaction().commit();
        Object oid = em.getObjectId(type);
        assertEquals(Id.class, JPAFacadeHelper.toOpenJPAObjectId(cmd, oid).getClass());
    }
View Full Code Here

        }
    }
   
    public void testRefresh() {
        if (getCacheEnabled()) {
            OpenJPAEntityManagerSPI em = getEntityManagerFactory().createEntityManager();
            CacheableEntity e1 = em.find(CacheableEntity.class, 1);
            XmlCacheableEntity e2 = em.find(XmlCacheableEntity.class, 1);
            assertNotNull(e1);
            assertNotNull(e2);
            int e1Version = e1.getVersion();
            int e2Version = e2.getVersion();

            String e1Sql = "UPDATE CacheableEntity SET VERSN=?1 WHERE ID=?2";
            String e2Sql = "UPDATE XmlCacheableEntity SET VERSN=?1 WHERE ID=?2";
            em.getTransaction().begin();
            assertEquals(1, em.createNativeQuery(e1Sql).setParameter(1, e1Version + 1).setParameter(2, e1.getId())
                .executeUpdate());
            assertEquals(1, em.createNativeQuery(e2Sql).setParameter(1, e2Version + 1).setParameter(2, e2.getId())
                .executeUpdate());
            em.getTransaction().commit();
            em.refresh(e1);
            em.refresh(e2);
            assertEquals(e1Version + 1, e1.getVersion());
            assertEquals(e2Version + 1, e2.getVersion());
            em.close();
        }
    }
View Full Code Here

        endTx(em);
        endEm(em);
    }

    public void testLowerClobFunc() {
        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) currentEntityManager();
        // some databases do not support case conversion on LOBs,
        // just skip this test case
        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
            .getDBDictionaryInstance();
        if (!dict.supportsCaseConversionForLob) {
            return;
        }
        startTx(em);

        CompUser user = em.find(CompUser.class, userid5);
        assertNotNull(user);
        assertEquals("Famzy", user.getName());

        String query = "UPDATE CompUser e SET " +
                "e.name = LOWER(e.name) WHERE LOWER(e.nameAsLob)='famzy'";

        int result = em.createQuery(query).executeUpdate();

        user = em.find(CompUser.class, userid5);
        em.refresh(user);
        assertNotNull(user);
        assertEquals("famzy", user.getName());

        endTx(em);
        endEm(em);
View Full Code Here

        endTx(em);
        endEm(em);
    }

    public void testUpperClobFunc() {
        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) currentEntityManager();
        // some databases do not support case conversion on LOBs,
        // just skip this test case
        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
            .getDBDictionaryInstance();
        if (!dict.supportsCaseConversionForLob) {
            return;
        }
        startTx(em);

        CompUser user = em.find(CompUser.class, userid5);
        assertNotNull(user);
        assertEquals("Famzy", user.getName());

        String query = "UPDATE CompUser e SET " +
                "e.name = UPPER(e.name) WHERE UPPER(e.nameAsLob)='FAMZY'";

        int result = em.createQuery(query).executeUpdate();

        user = em.find(CompUser.class, userid5);
        em.refresh(user);
        assertNotNull(user);
        assertEquals("FAMZY", user.getName());

        endTx(em);
        endEm(em);
View Full Code Here

    /**
     * This testcase was added for OPENJPA-1400.
     */
    public void testIsDetchedNoStateManagerZeroVersionField() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        NoDetachedStateEntityPropertyAccess property = em.find(NoDetachedStateEntityPropertyAccess.class, 1);
        NoDetachedStateEntityFieldAccess field = em.find(NoDetachedStateEntityFieldAccess.class, 1);
        em.close();

        PersistenceCapable pcProperty = (PersistenceCapable) property;
        PersistenceCapable pcField = (PersistenceCapable) field;

        assertTrue(pcProperty.pcIsDetached());
View Full Code Here

    /**
     * This testcase was added for OPENJPA-1400.
     */
    public void testPersistRelationshipToDetchedEntityZeroVersion() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        NoDetachedStateEntityPropertyAccess property = em.find(NoDetachedStateEntityPropertyAccess.class, 1);
        em.close();

        // Make sure we don't hit any exceptions when persisting a relationship to a detached
        // entity.
        em = emf.createEntityManager();
        em.getTransaction().begin();
        NoDetachedStateEntityFieldAccess field = em.find(NoDetachedStateEntityFieldAccess.class, 1);
        field.setRelationship(property);
        em.getTransaction().commit();
        em.close();

        // Make sure that the relationship was persisted
        em = emf.createEntityManager();
        field = em.find(NoDetachedStateEntityFieldAccess.class, 1);
        property = field.getRelationship();
        assertNotNull(property);
        assertEquals(1, property.getId());

    }
View Full Code Here

        assertEquals(1, property.getId());

    }

    void loadDB() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        em.getTransaction().begin();
        em.createNativeQuery("INSERT INTO NoDetachedStateEntityPropertyAccess (ID,VERSION) VALUES (1,0)")
            .executeUpdate();
        em.createNativeQuery("INSERT INTO NoDetachedStateEntityFieldAccess (ID,VERSION) VALUES (1,0)")
            .executeUpdate();
        em.getTransaction().commit();
        em.close();
    }
View Full Code Here

TOP

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

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.