Examples of CacheableEntity


Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

        // get entities from the cache and ensure their versions are as
        // expected.
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em = getEntityManagerFactory().createEntityManager();
        CacheableEntity ceFromEM = em.find(CacheableEntity.class, 1);
        XmlCacheableEntity xceFromEM = em.find(XmlCacheableEntity.class, 1);
        em.close();
        assertEquals(cacheUpdatedForTran1 ? version + 1 : version, ceFromEM.getVersion());
        assertEquals(cacheUpdatedForTran2 ? version + 1 : version, xceFromEM.getVersion());

        // get the data from the database. Version should always have been
        // updated in this case.
        em = getEntityManagerFactory().createEntityManager();
        em.setProperty(RETRIEVE_MODE_PROP, CacheRetrieveMode.BYPASS);
        CacheableEntity ceFromDB =
            (CacheableEntity) em.createNativeQuery("Select * from CacheableEntity where ID = 1", CacheableEntity.class)
                .getSingleResult();

        XmlCacheableEntity xceFromDB =
            (XmlCacheableEntity) em.createNativeQuery("Select * from XmlCacheableEntity where ID = 1",
                XmlCacheableEntity.class).getSingleResult();

        assertEquals(version + 1, ceFromDB.getVersion());
        assertEquals(version + 1, xceFromDB.getVersion());
        em.close();
    }
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

    }
   
    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

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

        // get entities from the cache and ensure their versions are as
        // expected.
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em = getEntityManagerFactory().createEntityManager();
        CacheableEntity ceFromEM = em.find(CacheableEntity.class, 1);
        XmlCacheableEntity xceFromEM = em.find(XmlCacheableEntity.class, 1);
        em.close();
        assertEquals(cacheUpdatedForTran1 ? version + 1 : version, ceFromEM.getVersion());
        assertEquals(cacheUpdatedForTran2 ? version + 1 : version, xceFromEM.getVersion());

        // get the data from the database. Version should always have been
        // updated in this case.
        em = getEntityManagerFactory().createEntityManager();
        em.setProperty(RETRIEVE_MODE_PROP, CacheRetrieveMode.BYPASS);
        CacheableEntity ceFromDB =
            (CacheableEntity) em.createNativeQuery("Select * from CacheableEntity where ID = 1", CacheableEntity.class)
                .getSingleResult();

        XmlCacheableEntity xceFromDB =
            (XmlCacheableEntity) em.createNativeQuery("Select * from XmlCacheableEntity where ID = 1",
                XmlCacheableEntity.class).getSingleResult();

        assertEquals(version + 1, ceFromDB.getVersion());
        assertEquals(version + 1, xceFromDB.getVersion());
        em.close();
    }
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

    }
   
    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

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

        // get entities from the cache and ensure their versions are as
        // expected.
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em = getEntityManagerFactory().createEntityManager();
        CacheableEntity ceFromEM = em.find(CacheableEntity.class, 1);
        XmlCacheableEntity xceFromEM = em.find(XmlCacheableEntity.class, 1);
        em.close();
        assertEquals(cacheUpdatedForTran1 ? version + 1 : version, ceFromEM.getVersion());
        assertEquals(cacheUpdatedForTran2 ? version + 1 : version, xceFromEM.getVersion());

        // get the data from the database. Version should always have been
        // updated in this case.
        em = getEntityManagerFactory().createEntityManager();
        em.setProperty(RETRIEVE_MODE_PROP, CacheRetrieveMode.BYPASS);
        CacheableEntity ceFromDB =
            (CacheableEntity) em.createNativeQuery("Select * from CacheableEntity where ID = 1", CacheableEntity.class)
                .getSingleResult();

        XmlCacheableEntity xceFromDB =
            (XmlCacheableEntity) em.createNativeQuery("Select * from XmlCacheableEntity where ID = 1",
                XmlCacheableEntity.class).getSingleResult();

        assertEquals(version + 1, ceFromDB.getVersion());
        assertEquals(version + 1, xceFromDB.getVersion());
        em.close();
    }
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

    }
   
    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

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

    public void testCacheRefreshModeRefresh() {

        EntityManager em = emf.createEntityManager();
       
        // Create a new cacheable entity
        CacheableEntity ce = createEntity(em);
        int ceid = ce.getId();

        // Clear the L1
        em.clear();
       
        // Clear the L2 cache
        Cache cache = emf.getCache();
        cache.evictAll();
        assertFalse(cache.contains(CacheableEntity.class, ceid));

        // Find the entity, reloading it into the L2
        em.getTransaction().begin();
        ce = em.find(CacheableEntity.class, ceid);
        assertTrue(em.contains(ce));
        assertTrue(cache.contains(CacheableEntity.class, ceid));
        assertTrue(em.getLockMode(ce) == LockModeType.NONE);
        assertEquals(ce.getName(), "Cached Entity");
        assertEquals(ce.getVersion(), 1);
        em.getTransaction().commit();

        // Create a new EMF -WITHOUT- the L2 enabled.  If the L2 was enabled, the
        // sjvm remote commit provider would evict the entity upon update, throwing
        // off the intent of this variation.
        EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
            "openjpa.LockManager", "mixed",
            "openjpa.ConnectionFactoryProperties", "PrintParameters=true");
        EntityManager em2 = emf2.createEntityManager();
       
        // Find + lock, then update the entity and commit
        em2.getTransaction().begin();
        CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        ce2.setName("Updated Cached Entity");
        em2.getTransaction().commit();
        em2.close();
        emf2.close();

        // Refresh the entity - this will load the entity into the L1 and with storeMode=REFRESH,
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

    public void testCacheRefreshModeRefreshDelete() {

        EntityManager em = emf.createEntityManager();

        // Create a new cachable entity
        CacheableEntity ce = createEntity(em);
        int ceid = ce.getId();

        // Clear the L2 cache
        Cache cache = emf.getCache();
        cache.evictAll();
        assertFalse(cache.contains(CacheableEntity.class, ceid));

        // Find the entity, reloading it into the L2
        em.getTransaction().begin();
        ce = em.find(CacheableEntity.class, ceid);
        assertTrue(em.contains(ce));
        assertTrue(cache.contains(CacheableEntity.class, ceid));
        assertTrue(em.getLockMode(ce) == LockModeType.NONE);
        assertEquals(ce.getName(), "Cached Entity");
        assertEquals(ce.getVersion(), 1);
        em.getTransaction().commit();

        // Create a new EMF -WITHOUT- the L2 enabled.  If the L2 was enabled, the
        // sjvm remote commit provider would evict the entity upon delete, throwing
        // off the intent of this variation.
        EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
            "openjpa.LockManager", "mixed",
            "openjpa.ConnectionFactoryProperties", "PrintParameters=true");
        EntityManager em2 = emf2.createEntityManager();

        // Find and delete the entity in a separate context with no L2 configured
        em2.getTransaction().begin();
        CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid);
        assertNotNull(ce2);
        em2.remove(ce2);
        em2.getTransaction().commit();
        em2.close();
        emf2.close();
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

        em.close();
  }

  private CacheableEntity createEntity(EntityManager em) {
        CacheableEntity ce = new CacheableEntity();
        int ceid = new Random().nextInt();
        ce.setId(ceid);
        ce.setName("Cached Entity");

        // Persist the new cachable entity
        em.getTransaction().begin();
        em.persist(ce);
        em.getTransaction().commit();
View Full Code Here

Examples of org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity

        // get entities from the cache and ensure their versions are as
        // expected.
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em = getEntityManagerFactory().createEntityManager();
        CacheableEntity ceFromEM = em.find(CacheableEntity.class, 1);
        XmlCacheableEntity xceFromEM = em.find(XmlCacheableEntity.class, 1);
        em.close();
        assertEquals(cacheUpdatedForTran1 ? version + 1 : version, ceFromEM.getVersion());
        assertEquals(cacheUpdatedForTran2 ? version + 1 : version, xceFromEM.getVersion());

        // get the data from the database. Version should always have been
        // updated in this case.
        em = getEntityManagerFactory().createEntityManager();
        em.setProperty(RETRIEVE_MODE_PROP, CacheRetrieveMode.BYPASS);
        CacheableEntity ceFromDB =
            (CacheableEntity) em.createNativeQuery("Select * from CacheableEntity where id = 1", CacheableEntity.class)
                .getSingleResult();

        XmlCacheableEntity xceFromDB =
            (XmlCacheableEntity) em.createNativeQuery("Select * from XmlCacheableEntity where id = 1",
                XmlCacheableEntity.class).getSingleResult();

        assertEquals(version + 1, ceFromDB.getVersion());
        assertEquals(version + 1, xceFromDB.getVersion());
        em.close();
    }
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.