Package org.apache.openjpa.persistence

Examples of org.apache.openjpa.persistence.OpenJPAEntityManager


        em.getTransaction().commit();
        em.close();
    }

    public void testAccessIdBeforeCommit() {
        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        UnenhancedIdentityIdPropertyAccess o =
            new UnenhancedIdentityIdPropertyAccess();
        em.persist(o);
        em.getObjectId(o);
        em.getTransaction().commit();
        em.close();
    }
View Full Code Here


    public void setUp() throws Exception {
        setUp("openjpa.DataCache", "true",
            "openjpa.RemoteCommitProvider", "sjvm",
            AllFieldTypes.class, CascadeParent.class, CascadeChild.class);

        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        AllFieldTypes pc = new AllFieldTypes();
        pc.setStringField("DeleteMe");
        em.persist(pc);
        oid = em.getObjectId(pc);
        em.getTransaction().commit();
        em.close();
    }
View Full Code Here

        em.getTransaction().commit();
        em.close();
    }

    public void testBulkDelete() {
        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        List result = em.createQuery("SELECT o FROM AllFieldTypes o")
            .getResultList();
        assertEquals(1, result.size());
        em.createQuery("DELETE FROM AllFieldTypes o").executeUpdate();
        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();

        // this assumes that we invalidate the cache, rather than update it
        // according to the bulk rule.
        assertFalse(OpenJPAPersistence.cast(emf).getStoreCache()
            .contains(AllFieldTypes.class, oid));

        assertNull(em.find(AllFieldTypes.class, oid));
        em.close();
    }
View Full Code Here

        assertNull(em.find(AllFieldTypes.class, oid));
        em.close();
    }

    public void testBulkUpdate() {
        OpenJPAEntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        List result = em.createQuery("SELECT o FROM AllFieldTypes o "
            + "WHERE o.intField = 0").getResultList();
        assertEquals(1, result.size());
        em.createQuery("UPDATE AllFieldTypes o SET o.intField = 10")
            .executeUpdate();
        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();

        // this assumes that we invalidate the cache, rather than update it
        // according to the bulk rule.
        assertFalse(OpenJPAPersistence.cast(emf).getStoreCache()
            .contains(AllFieldTypes.class, oid));

        em.close();
    }
View Full Code Here

        setUp("openjpa.DataCache", "true",
            "openjpa.RemoteCommitProvider", "sjvm",
            "openjpa.Log", "SQL=TRACE",
            OptimisticLockInstance.class, CLEAR_TABLES);

        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        OptimisticLockInstance pc = new OptimisticLockInstance("foo");
        em.persist(pc);
        em.getTransaction().commit();
        em.close();
    }
View Full Code Here

    public void testGetObjectIdOnUserDefinedSubclass() {
        getObjectIdHelper(false, true);
    }

    private void getObjectIdHelper(boolean sub, boolean userDefined) {
        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        UnenhancedType un = newInstance(sub);
        em.persist(un);
        em.getTransaction().commit();

        if (!userDefined) {
            em.close();
            em = emf.createEntityManager();
            un = em.find(getUnenhancedClass(), un.getId());
        }

        assertNotNull(em.getObjectId(un));
    }
View Full Code Here

    public void testSubclassOperationsOnOpenJPADefined() {
        opsHelper(true, false);
    }

    private void opsHelper(boolean sub, boolean userDefined) {
        OpenJPAEntityManager em = null;
        try {
            UnenhancedType un = newInstance(sub);
            em = emf.createEntityManager();

            em.getTransaction().begin();
            em.persist(un);
            un.setStringField("bar");
            assertEquals("bar", un.getStringField());
            assertPersistenceContext(em, un, true, true, sub);
            em.flush();
            assertPersistenceContext(em, un, true, true, sub);
            assertTrue(un.getId() != 0);
            UnenhancedType un2 = em.find(getUnenhancedClass(), un.getId());
            assertSame(un, un2);
            em.getTransaction().commit();
            assertPersistenceContext(em, un, false, false, sub);
            un2 = em.find(getUnenhancedClass(), un.getId());
            assertSame(un, un2);

            if (!userDefined) {
                em.close();
                em = emf.createEntityManager();
            }

            un = em.find(getUnenhancedClass(), un.getId());
            assertNotNull(un);
            if (!userDefined)
                assertTrue(un instanceof PersistenceCapable);
            assertEquals("bar", un.getStringField());
            em.getTransaction().begin();
            un.setStringField("baz");
            assertPersistenceContext(em, un, true, true, sub);
            assertEquals("baz", un.getStringField());

            if (sub)
                ((UnenhancedSubtype) un).setIntField(17);

            assertTrue(em.isDirty(un));
           
            em.getTransaction().commit();

            // make sure that the values are still up-to-date after
            // the commit happens
            assertEquals("baz", un.getStringField());
           
            em.close();

            em = emf.createEntityManager();
            un = em.find(getUnenhancedClass(), un.getId());
            assertNotNull(un);
            assertTrue(un instanceof PersistenceCapable);
            assertEquals("baz", un.getStringField());
            if (sub)
                assertEquals(17, ((UnenhancedSubtype) un).getIntField());
            em.close();
        } finally {
            if (em != null && em.getTransaction().isActive())
                em.getTransaction().rollback();
            if (em != null && em.isOpen())
                em.close();
        }
    }
View Full Code Here

                || em.getPersistedClasses().contains(cls));
        }
    }

    public void testRelations() {
        OpenJPAEntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        UnenhancedSubtype un = newUnenhancedSubclassInstance();
        em.persist(un);
        un.setStringField("aoeu");
        UnenhancedSubtype related = newUnenhancedSubclassInstance();
        un.setRelated(related);
        related.setStringField("snth");
        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();
        un = (UnenhancedSubtype) em.find(getUnenhancedClass(), un.getId());
        assertEquals("aoeu", un.getStringField());
        assertNotNull(un.getRelated());
        assertEquals("snth", un.getRelated().getStringField());
        em.close();
    }
View Full Code Here

        evictionHelper(false);
    }

    private void evictionHelper(boolean userDefined)
        throws NoSuchFieldException, IllegalAccessException {
        OpenJPAEntityManager em = emf.createEntityManager();
        UnenhancedType un = newUnenhancedInstance();
        em.getTransaction().begin();
        em.persist(un);
        em.getTransaction().commit();

        if (!userDefined) {
            em.close();
            em = emf.createEntityManager();
            un = em.find(getUnenhancedClass(), un.getId());
            assertTrue(getUnenhancedClass() != un.getClass());
        }

        em.evict(un);
        OpenJPAStateManager sm = (OpenJPAStateManager)
            ImplHelper.toPersistenceCapable(un, null).pcGetStateManager();

        // we only expect lazy loading to work when we can redefine classes
        // or when accessing a property-access record that OpenJPA created.
        if (ClassRedefiner.canRedefineClasses()
            || (!userDefined && sm.getMetaData().getAccessType()
                != ClassMetaData.ACCESS_FIELD)) {

            assertFalse(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));

            // make sure that the value was cleared...
            Field field = getUnenhancedClass().getDeclaredField(
                isFieldAccessTest() ? "stringField" : "sf");
            field.setAccessible(true);
            assertEquals(null, field.get(un));

            // ... and that it gets reloaded properly
            assertEquals("foo", un.getStringField());
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));
        } else {
            // unredefined properties with user-defined instance, or any
            // unredefined field access
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));

            // make sure that the value was not cleared
            Field field = getUnenhancedClass().getDeclaredField(
                isFieldAccessTest() ? "stringField" : "sf");
            field.setAccessible(true);
            assertEquals("foo", field.get(un));
        }

        em.close();
    }
View Full Code Here

    protected abstract boolean isFieldAccessTest();

    public void testLazyLoading()
        throws NoSuchFieldException, IllegalAccessException {
        OpenJPAEntityManager em = emf.createEntityManager();
        UnenhancedType un = newUnenhancedInstance();
        em.getTransaction().begin();
        em.persist(un);
        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();
        un = em.find(getUnenhancedClass(), un.getId());
        assertTrue(getUnenhancedClass() != un.getClass());
        OpenJPAStateManager sm = (OpenJPAStateManager)
            ImplHelper.toPersistenceCapable(un, null).pcGetStateManager();

        // we only expect lazy loading to work when we can redefine classes
        // or when accessing a property-access record that OpenJPA created.
        if (ClassRedefiner.canRedefineClasses()
            || (sm.getMetaData().getAccessType() != ClassMetaData.ACCESS_FIELD))
        {
            assertFalse(sm.getLoaded()
                .get(sm.getMetaData().getField("lazyField").getIndex()));

            // make sure that the value was cleared
            Field field = getUnenhancedClass().getDeclaredField("lazyField");
            field.setAccessible(true);
            assertEquals(null, field.get(un));
        } else {
            // unredefined field access
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("lazyField").getIndex()));

            // make sure that the value was loaded already
            Field field = getUnenhancedClass().getDeclaredField("lazyField");
            field.setAccessible(true);
            assertEquals("lazy", field.get(un));
        }

        // make sure that the value is available, one way or another
        assertEquals("lazy", un.getLazyField());
        assertTrue(sm.getLoaded()
            .get(sm.getMetaData().getField("lazyField").getIndex()));

        em.close();
    }
View Full Code Here

TOP

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

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.