Package org.apache.openjpa.persistence

Examples of org.apache.openjpa.persistence.OpenJPAEntityManagerSPI


//            em.close();
//        }
//    }

    public void test() throws Exception {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        try {
            String updatedName = "updatedName_" + System.currentTimeMillis();
            IntVersionEntity detachedIve = new IntVersionEntity(_ive.getId());
            NoVersionEntity detachedNve = new NoVersionEntity(_nve.getId());
            IntegerVersionEntity detachedIntegerVe = new IntegerVersionEntity(_integerVe.getId());

            detachedIntegerVe.setName(updatedName);
            detachedNve.setName(updatedName);
            detachedIve.setName(updatedName);

            em.getTransaction().begin();
            em.merge(detachedIntegerVe);
            em.merge(detachedNve);
            em.merge(detachedIve);
            em.getTransaction().commit();

            em.clear();

            detachedIntegerVe = em.find(IntegerVersionEntity.class, _integerVe.getId());
            detachedNve = em.find(NoVersionEntity.class, _nve.getId());
            detachedIve = em.find(IntVersionEntity.class, _ive.getId());

            // Make sure the updated values were persisted
            assertEquals(detachedIntegerVe.getName(), updatedName);
            assertEquals(detachedNve.getName(), updatedName);
            assertEquals(detachedIve.getName(), updatedName);

        } finally {
            if (em.getTransaction().isActive()) {
                em.getTransaction().commit();
            }
            em.close();
        }
    }
View Full Code Here


        super.setUp(props);
        createData();
    }

    public void test() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        StoreManager store = ((EntityManagerImpl) em).getBroker().getStoreManager().getDelegate();

        FetchPlan fp = getFetchPlan(em);
        try {
            sql.clear();

            if (store instanceof JDBCStoreManager == false) {
                fail("StoreManager is not an instanceof JDBCStoreManager");
            }
            // Set this JDBCFetchPlan property so that we will select FKs for fields that are in the DFG, but not
            // included in the current load. If this property isn't set, the FK for eagerOneToOneOwner will not be
            // selected.
             ((JDBCFetchPlan)fp).setIgnoreDfgForFkSelect(true);
            // Remove all relationships
            fp.removeField(OptSelectEntity.class, "eagerOneToOne");
            fp.removeField(OptSelectEntity.class, "eagerOneToOneOwner");
            fp.removeField(OptSelectEntity.class, "lazyOneToOne");
            fp.removeField(OptSelectEntity.class, "lazyOneToOneOwner");

            OptSelectEntity ee1 = em.find(OptSelectEntity.class, e1.getId());

            // Make sure our sql has no joins
            assertEquals(1, sql.size());
            String s = sql.get(0);
            assertFalse(s.contains("JOIN") && s.contains("join"));

            // Check to see how many fks(intermediate fields) we selected.
            StateManagerImpl smi = ((StateManagerImpl) ((PersistenceCapable) ee1).pcGetStateManager());
            ClassMetaData cmd =
                em.getConfiguration().getMetaDataRepositoryInstance().getMetaData(OptSelectEntity.class, null, true);
            int fks = 0;
            for (FieldMetaData fmd : cmd.getFields()) {
                if (smi.getIntermediate(fmd.getIndex()) != null) {
                    fks++;
                }
            }
            // We expected to find 2 FKs. One for each of the owners (lazyOneToOneOwner and eagerOneToOneOwner)
            assertEquals(2, fks);
        } finally {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            if (em.isOpen()) {
                em.close();
            }
        }
    }
View Full Code Here

        }
        return fp;
    }

    void createData() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            e1 = new OptSelectEntity();
            e2 = new OptSelectEntity();

            e1.setEagerOneToOne(e2);
            e2.setEagerOneToOneOwner(e2);

            e1.setLazyOneToOne(e2);
            e2.setLazyOneToOneOwner(e1);

            em.persistAll(e1, e2);

            em.getTransaction().commit();
        } finally {
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
            if (em.isOpen())
                em.close();
        }
    }
View Full Code Here

        em.getTransaction().commit();
    }
   
    public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        em.getTransaction().begin();       
        lockClause = getForUpdateClause();
        OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
        FetchPlan fp = q.getFetchPlan();
        fp.setReadLockMode(LockModeType.NONE);
        assertClauseAbsentInSQL(lockClause, q);
       
        q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0);
        fp = q.getFetchPlan();
        fp.setReadLockMode(LockModeType.NONE);
        assertClauseAbsentInSQL(lockClause, q);
       
        em.getTransaction().commit();
    }
View Full Code Here

   
    /*
     * Optimistic=true, lock mode = none set explicitly on the fetch plan.
     */
    public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        em.getTransaction().begin();
        lockClause = getForUpdateClause();       
        OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
        FetchPlan fp = q.getFetchPlan();
        fp.setReadLockMode(LockModeType.NONE);
        assertClauseAbsentInSQL(lockClause, q);
       
        q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0);
        fp = q.getFetchPlan();
        fp.setReadLockMode(LockModeType.NONE);
        assertClauseAbsentInSQL(lockClause, q);
       
        em.getTransaction().commit();
        em.close();
    }
View Full Code Here

                "simple",
                "org/apache/openjpa/persistence/validation/persistence.xml",
                prop);
        assertNotNull(emf);
        try {
            final OpenJPAEntityManagerSPI em = emf.createEntityManager();
            final OpenJPAEntityManagerSPI em2 = emf.createEntityManager();
            UniqueLifecycleListener l1 = new UniqueLifecycleListener();
            UniqueLifecycleListener l2 = new UniqueLifecycleListener();
            em.addLifecycleListener(l1, (Class<?>[])null);
            em2.addLifecycleListener(l2, (Class<?>[])null);

            l1.assertCounts(0, 0, 0, 0);
            l2.assertCounts(0, 0, 0, 0);

            em.getTransaction().begin();
            SimpleEntity e1 = new SimpleEntity();
            em.persist(e1);
            l1.assertCounts(1, 1, 0, 0);
            l2.assertCounts(0, 0, 0, 0);

            em2.getTransaction().begin();
            SimpleEntity e2 = new SimpleEntity();
            em2.persist(e2);
            l1.assertCounts(1, 1, 0, 0);
            l2.assertCounts(1, 1, 0, 0);

            em2.getTransaction().commit();
            l1.assertCounts(1, 1, 0, 0);
            l2.assertCounts(1, 1, 1, 1);

            em.getTransaction().commit();
            l1.assertCounts(1, 1, 1, 1);
View Full Code Here

            dict instanceof SolidDBDictionary || dict instanceof HSQLDictionary)) {
            closeEMF(tempEmf);
            return;
        }
       
        OpenJPAEntityManagerSPI em = tempEmf.createEntityManager();
        String[] schemas =
            { "SCHEMA1", "SCHEMA2", "SCHEMA3", "SCHEMA3G", "SCHEMA4G" };
        for (String schema : schemas) {
            try {
                em.getTransaction().begin();
                Query q = em.createNativeQuery("create schema " + schema);
                q.executeUpdate();
                em.getTransaction().commit();
            } catch (PersistenceException e) {         
                em.getTransaction().rollback();
            }
        }
        closeEM(em);
        closeEMF(tempEmf);
    }
View Full Code Here

    public void testEmptyResultTimeout() {
        // Not all databases support GenerationType.IDENTITY column(s)
        if (!((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance().supportsAutoAssign) {
            return;
        }
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        String query = "select p from PartBase p where p.cost > ?1";
        // execute a query that needs to return zero results
        Query q = em.createQuery(query);
        q.setParameter(1, 100000);
        List l = q.getResultList();
        assertEquals(0, l.size());    

        // Create a new Entity that has the PartBase accesspath. Has a newer timestamp than our query
        em.getTransaction().begin();
        em.persist(new PartBase());
        em.getTransaction().commit();

        // Make sure that our sql listener is working
        assertTrue(_sql.size() > 0);
        _sql.clear();
       
        q = em.createQuery(query);
        q.setParameter(1, 100000);
        q.getResultList();
       
        // Make sure that we execute sql. This means that the query was properly kicked out of the cache.
        assertEquals(1, _sql.size());
        em.close();

    }
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

    /**
     * Create sequence so that the test does not require manual intervention in database.
     */
    private void createSequence(String sequence) {
        OpenJPAEntityManagerFactorySPI factorySPI = createEMF();
        OpenJPAEntityManagerSPI em = factorySPI.createEntityManager();

        try {
            em.getTransaction().begin();
            Query q = em.createNativeQuery("CREATE SEQUENCE " + sequence + " START WITH 1");
            q.executeUpdate();
            em.getTransaction().commit();
        } catch (PersistenceException e) {         
            // Sequence probably exists.
            em.getTransaction().rollback();
        }
        closeEM(em);
        closeEMF(factorySPI);
    }
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.