Package javax.persistence

Examples of javax.persistence.PersistenceUnitUtil


      if (list == null || list.isEmpty()) {
        return Collections.emptySet();
      }

      PersistenceUnitUtil util = emf.getPersistenceUnitUtil();

      Set<InternalCacheEntry> result = new HashSet<InternalCacheEntry>(
          list.size());
      for (Object o : list) {
        Object key = util.getIdentifier(o);
        result.add(new ImmortalCacheEntry(key, o));
      }

      return result;
    } finally {
View Full Code Here


            "id"));
    }

   
    private void verifyIsLoadedEagerState(boolean loaded) {
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        assertSame(emf, puu);
        EntityManager em = emf.createEntityManager();
        EagerEntity ee = createEagerEntity();
       
        // Vfy LoadState is false for the unmanaged entity
        assertEquals(false, puu.isLoaded(ee));
        assertEquals(false, puu.isLoaded(ee,
            "id"));
       
        em.getTransaction().begin();
        em.persist(ee);
        em.getTransaction().commit();
        em.clear();
       
        if (loaded)
            ee = em.find(EagerEntity.class, ee.getId());
        else
            ee = em.getReference(EagerEntity.class, ee.getId());
       
        assertEquals(loaded, puu.isLoaded(ee));
        assertEquals(loaded, puu.isLoaded(ee, "id"));
        assertEquals(loaded, puu.isLoaded(ee, "name"));
        assertEquals(loaded, puu.isLoaded(ee, "eagerEmbed"));
        assertEquals(false, puu.isLoaded(ee, "transField"));
       
        em.close();
    }
View Full Code Here

       
        em.close();
    }

    private void verifyIsLoadedLazyState(boolean loaded) {
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        assertSame(emf, puu);
        EntityManager em = emf.createEntityManager();
        LazyEntity le = createLazyEntity();
       
        // Vfy LoadState is false for the unmanaged entity
        assertEquals(false, puu.isLoaded(le));
        assertEquals(false, puu.isLoaded(le,"id"));
       
        em.getTransaction().begin();
        em.persist(le);
        em.getTransaction().commit();
        em.clear();
       
        // Use find or getReference based upon expected state
        if (loaded)
            le = em.find(LazyEntity.class, le.getId());
        else
            le = em.getReference(LazyEntity.class, le.getId());
       
        assertEquals(loaded, puu.isLoaded(le));
        assertEquals(loaded, puu.isLoaded(le, "id"));

        // Name is lazy fetch so it should not be loaded
        assertEquals(false, puu.isLoaded(le, "name"));
        assertEquals(loaded, puu.isLoaded(le, "lazyEmbed"));
        assertEquals(false, puu.isLoaded(le, "transField"));
       
        em.close();
    }
View Full Code Here

    /*
     * Verifies that an entity and attributes are considered loaded if they
     * are assigned by the application.
     */
    public void testIsApplicationLoaded() {
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        assertSame(emf, puu);
        EntityManager em = emf.createEntityManager();
        EagerEntity ee = createEagerEntity();
       
        em.getTransaction().begin();
View Full Code Here

       
        em.close();
    }

    public void testPCMapEager() {
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        EntityManager em = emf.createEntityManager();
       
        MapValEntity mve = new MapValEntity();
        mve.setIntVal(10);
        MapKeyEmbed mke = new MapKeyEmbed();
        mke.setFirstName("Jane");
        mke.setLastName("Doe");
       
        MapEntity me = new MapEntity();

        assertEquals(false, puu.isLoaded(me));
        assertEquals(false, puu.isLoaded(me,
            "mapValEntity"));
        assertEquals(false, puu.isLoaded(me,
            "mapEntities"));

        assertEquals(false, puu.isLoaded(mve));

        // Create a circular ref
        me.setMapValEntity(mve);
        mve.setMapEntity(me);

        HashMap<MapKeyEmbed, MapValEntity> hm =
            new HashMap<MapKeyEmbed, MapValEntity>();
       
        hm.put(mke, mve);
        me.setMapEntities(hm);

        em.getTransaction().begin();
        em.persist(me);
        em.getTransaction().commit();
       
        assertEquals(true, puu.isLoaded(me));
        assertEquals(true, puu.isLoaded(me,
            "mapValEntity"));
        assertEquals(true, puu.isLoaded(me,
            "mapEntities"));

        assertEquals(true, puu.isLoaded(mve));
       
        em.close();
    }
View Full Code Here

     * Verify load state is not loaded for null relationships or relationships
     * set to null.
     */
    public void testSetNullLazyRelationship() {

        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        EntityManager em = emf.createEntityManager();

        try {
            OneToEntity ote = new OneToEntity();
            assertFalse(puu.isLoaded(ote, "toManyLazy"));
            em.getTransaction().begin();
            em.persist(ote);
            em.getTransaction().commit();
            em.clear();
            ote = em.find(OneToEntity.class, ote.getId());
            // Field is lazy and not immediately loaded by the application
            assertFalse(puu.isLoaded(ote, "toManyLazy"));
            // Force load the lazy field
            ote.getToManyLazy();
            assertTrue(puu.isLoaded(ote, "toManyLazy"));
           
            OneToEntity ote2 = new OneToEntity();
            em.getTransaction().begin();
            em.persist(ote2);
            em.getTransaction().commit();
            // Field gets set to loaded upon commit
            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
            em.clear();
            ote2 = em.find(OneToEntity.class, ote2.getId());
           
            // Field is lazy and not immediately loaded by the application
            assertFalse(puu.isLoaded(ote2, "toManyLazy"));
           
            // Load by application
            List<ToManyLazy> tmes = new ArrayList<ToManyLazy>();
            for (int i = 0; i < 5; i++) {
                tmes.add(new ToManyLazy("ToMany" + i));
            }
            em.getTransaction().begin();
            ote2.setToManyLazy(tmes);
            // App loaded before commit
            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
            em.getTransaction().commit();
            // Still loaded after commit
            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
           
            // Set to null - still loaded per spec.
            em.getTransaction().begin();
            ote2.setToManyLazy(null);
            // Considered loaded before commit
            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
            em.getTransaction().commit();
            //Loaded after commit
            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
        }
        finally {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
View Full Code Here

        em.close();
    }

    public void testSetNullEagerRelationship() {

        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        EntityManager em = emf.createEntityManager();

        try {
            OneToEntity ote = new OneToEntity();
            assertFalse(puu.isLoaded(ote, "toManyEager"));
            em.getTransaction().begin();
            em.persist(ote);
            em.getTransaction().commit();
            em.clear();
            ote = em.find(OneToEntity.class, ote.getId());
            // Field is eager and is immediately loaded by the application
            assertTrue(puu.isLoaded(ote, "toManyEager"));
           
            OneToEntity ote2 = new OneToEntity();
            em.getTransaction().begin();
            em.persist(ote2);
            // Field is null by default, but after persist, it is treated as loaded.
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
            em.getTransaction().commit();
            // Field gets set to loaded upon commit
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
            em.clear();
            ote2 = em.find(OneToEntity.class, ote2.getId());
           
            // Field is eager and is immediately loaded by the application
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
           
            // Load by application
            List<ToManyEager> tmes = new ArrayList<ToManyEager>();
            for (int i = 0; i < 5; i++) {
                tmes.add(new ToManyEager("ToMany" + i));
            }
            em.getTransaction().begin();
            ote2.setToManyEager(tmes);
            // App loaded before commit
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
            em.getTransaction().commit();
            // Still loaded after commit
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
           
            // Set to null - still loaded per spec.
            em.getTransaction().begin();
            ote2.setToManyEager(null);
            // Entity is considered loaded before commit
            assertTrue(puu.isLoaded(ote2));
            // Attribute is considered loaded before commit
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
            em.getTransaction().commit();
            //Loaded after commit
            assertTrue(puu.isLoaded(ote2, "toManyEager"));
        }
        finally {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
View Full Code Here

        }
        em.close();
    }

    public void testBasicTypeNotLoaded() {
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        EntityManager em = emf.createEntityManager();
        EagerEntity ee = createEagerEntity();
        int id = ee.getId();
       
        em.getTransaction().begin();
        em.persist(ee);
        em.getTransaction().commit();
        em.clear();
        // name is not eagerly loaded, only eagerEmbed is eagerly loaded
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
        kem.getFetchPlan().resetFetchGroups().removeFetchGroup("default")
            .addField(EagerEntity.class, "eagerEmbed");
        ee = em.find(EagerEntity.class, id);
        assertEquals(true, puu.isLoaded(ee));
    }
View Full Code Here

        em.persist(a);
        em.getTransaction().begin();
        em.getTransaction().commit();
        em.clear();
       
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
        // do not fetch emb
        kem.getFetchPlan().resetFetchGroups().removeFetchGroup("default")
            .addField(EntityA_Embed.class, "name")
            .addField(EntityA_Embed.class, "age");
        a = em.find(EntityA_Embed.class, ID);
        assertNotNull(a);
        Embed embed = a.getEmbed();
        assertNull(embed);
        assertFalse(puu.isLoaded(a, "embed"));
        em.close();
    }
View Full Code Here

        em.persist(a);
        em.getTransaction().begin();
        em.getTransaction().commit();
        em.clear();
       
        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
        // do not fetch emb
        kem.getFetchPlan().resetFetchGroups().removeFetchGroup("default")
            .addField(EntityA_Embed.class, "name")
            .addField(EntityA_Embed.class, "age");
        a = em.find(EntityA_Embed.class, ID);
        assertNotNull(a);
        Embed embed = a.getEmbed();
        assertNull(embed);
        assertFalse(puu.isLoaded(a, "embed"));
    }
View Full Code Here

TOP

Related Classes of javax.persistence.PersistenceUnitUtil

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.