Package javax.persistence

Examples of javax.persistence.PersistenceUnitUtil


    /*
     * 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

  private Serializable identifier;
  private List<Object> constructorParams;
 
  public JpaLoadableModel(EntityManagerFactory entityManagerFactory, T entity) {
    super();
    PersistenceUnitUtil util = entityManagerFactory.getPersistenceUnitUtil();
   
    this.entityManagerFactory = entityManagerFactory;
    this.entityClass = (Class<T>) entity.getClass();
    this.identifier = (Serializable) util.getIdentifier(entity);
   
    setObject(entity);
  }
View Full Code Here

  @Override
  protected void onDetach() {
    super.onDetach();
   
    T entity = getObject();
    PersistenceUnitUtil persistenceUtil = entityManagerFactory.getPersistenceUnitUtil();
   
    if(entity == null) return;
   
    identifier = (Serializable) persistenceUtil.getIdentifier(entity);   
  }
View Full Code Here

   
    entityManager.getTransaction().begin();
    entityManager.persist(person);
    entityManager.getTransaction().commit();
   
    PersistenceUnitUtil util = entityManagerFactory.getPersistenceUnitUtil();
   
    JpaLoadableModel<Person> model = new JpaLoadableModel(entityManagerFactory, person);
    model.detach();
   
    Assert.assertNotNull(model.getObject())
View Full Code Here

    }

    @Test
    public void testIsLoaded()
    {
        PersistenceUnitUtil utils = emf.getPersistenceUnitUtil();
       
        Assert.assertNotNull(utils);
        Assert.assertFalse(utils.isLoaded(null));
    }
View Full Code Here

    }
   
    @Test
    public void testIsLoadedWithoutReference()
    {
        PersistenceUnitUtil utils = emf.getPersistenceUnitUtil();
       
        Person p = new Person();
        p.setAge(32);
        p.setPersonId("1");
     
        Assert.assertNotNull(utils);
        Assert.assertTrue(utils.isLoaded(p, "personId"));
        Assert.assertFalse(utils.isLoaded(null, "personName"));
    }
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.