Package org.apache.openjpa.persistence

Examples of org.apache.openjpa.persistence.OpenJPAEntityManagerSPI


    /**
     * Validates that a mapped superclass using field access and an entity
     * subclass using property access get mapped properly.
     */
    public void testAbstractMappedSuperField() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
       
        PropertySub ps = new PropertySub();
        // Call super setter with underlying field access
        ps.setName("AbsMappedSuperName");
        // Call base setter with property access
        Date now = new Date();
        ps.setCreateDate(now);
       
        em.getTransaction().begin();
        em.persist(ps);
        em.getTransaction().commit();
        em.clear();
       
        // This value of a persistent field was set using the setter
        // above, but this query will use the property name to verify that
        // propety access is in use.
        Query qry = em.createNamedQuery("PropertySub.query");
        qry.setParameter("id", ps.getId());
        qry.setParameter("name", "AbsMappedSuperName");
        qry.setParameter("crtDate", now);
        PropertySub ps2 =
            (PropertySub)qry.getSingleResult();
        assertEquals(ps, ps2);
        assertEquals(ps2.getName(), "AbsMappedSuperName");
        assertEquals(ps2.getCreateDate().toString(), now.toString());

        try {
            qry = em.createNamedQuery("PropertySub.badQuery");
            qry.setParameter("id", ps.getId());
            qry.setParameter("name", "AbsMappedSuperName");
            qry.setParameter("crtDate", now);
            qry.getSingleResult();
            fail("Usage of this query should have thrown an exception");
        }
        catch (Exception e) {
            assertExceptionMessage(e, ArgumentException.class,
                "No field named \"crtDate\" in \"PropertySub\"",
                "[createDate, id, name]");
        }

        em.close();
    }
View Full Code Here


    /**
     * Validates that a mapped superclass using property access and an entity
     * subclass using field access get mapped properly.
     */
    public void testAbstractMappedSuperProperty() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
       
        FieldSub fs = new FieldSub();
        // Call super setter with underlying field access
        fs.setName("AbsMappedSuperName");
        // Call base setter with property access
        Date now = new Date();
        fs.setCreateDate(now);
       
        em.getTransaction().begin();
        em.persist(fs);
        em.getTransaction().commit();
        em.clear();
       
        // This value of a persistent field was set using the setter
        // above, but this query will use the property name to verify that
        // propety access is in use.
        Query qry = em.createNamedQuery("FieldSub.query");
        qry.setParameter("id", fs.getId());
        qry.setParameter("name", "AbsMappedSuperName");
        qry.setParameter("crtDate", now);
        FieldSub fs2 =
            (FieldSub)qry.getSingleResult();
        assertEquals(fs, fs2);
        assertEquals(fs2.getName(), "AbsMappedSuperName");
        assertEquals(fs2.getCreateDate().toString(), now.toString());

        try {
            qry = em.createNamedQuery("FieldSub.badQuery");
            qry.setParameter("id", fs.getId());
            qry.setParameter("name", "AbsMappedSuperName");
            qry.setParameter("crtDate", now);
            qry.getSingleResult();
            fail("Usage of this query should have thrown an exception");
        }
        catch (Exception e) {
            assertExceptionMessage(e, ArgumentException.class,
                "No field named \"createDate\" in \"FieldSub\"",
                "[crtDate, id, name]");
        }

        em.close();
    }
View Full Code Here

     * Validates that an mapped superclass using field access and an
     * entity subclass using property access get mapped properly.
     * The subclass uses a storage field in the superclass.
     */
    public void testMappedSuperField() {
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
       
        PropertySub2 ps = new PropertySub2();
        // Call super setter with underlying field access
        ps.setName("MappedSuperName");
        // Call base setter with property access
        Date now = new Date();
        ps.setCreateDate(now);
       
        em.getTransaction().begin();
        em.persist(ps);
        em.getTransaction().commit();
        em.clear();
       
        // This value of a persistent field was set using the setter
        // above, but this query will use the property name to verify that
        // propety access is in use.
        Query qry = em.createNamedQuery("PropertySub2.query");
        qry.setParameter("id", ps.getId());
        qry.setParameter("name", "MappedSuperName");
        qry.setParameter("crtDate", now);
        PropertySub2 ps2 =
            (PropertySub2)qry.getSingleResult();
        assertEquals(ps, ps2);
        assertEquals(ps2.getName(), "MappedSuperName");
        assertEquals(ps2.getCreateDate().toString(), now.toString());

        try {
            qry = em.createNamedQuery("PropertySub2.badQuery");
            qry.setParameter("id", ps.getId());
            qry.setParameter("name", "MappedSuperName");
            qry.setParameter("crtDate", now);
            qry.getSingleResult();
            fail("Usage of this query should have thrown an exception");
        }
        catch (Exception e) {
            assertExceptionMessage(e, ArgumentException.class,
                    "No field named \"crtDate\" in \"PropertySub2\"",
                    "[createDate, id, name]");
        }

        em.close();       
    }
View Full Code Here

    public void overridePropertyOnEM(String name, String value) {
        // use the default JndiName for the base EntityManagerFactory
        OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI)getEmf(name, defaultJndiName);
        assertNotNull(emf1);
        try {
            OpenJPAEntityManagerSPI em = emf1.createEntityManager();
            assertNotNull(em);

            EntityManager em1 = getEm(emf1, name, value);
            assertNotNull(em1);

            // 'prove' that we're using a different database by inserting the same row
            em.getTransaction().begin();
            em.persist(new Person(1, "em"));
            em.getTransaction().commit();

            em1.getTransaction().begin();
            em1.persist(new Person(1, "em1"));
            em1.getTransaction().commit();

            em.clear();
            em1.clear();

            Person p = em.find(Person.class, 1);
            Person p1 = em1.find(Person.class, 1);
            assertNotSame(p, p1);
            assertEquals("em", p.getName());
            assertEquals("em1", p1.getName());

            em.clear();
            em1.clear();

            // make sure inserting the same row again fails.
            em.getTransaction().begin();
            em.persist(new Person(1));
            try {
                em.getTransaction().commit();
                fail("Should not be able to commit the same row a second time");
            } catch (RollbackException rbe) {
                assertTrue(rbe.getCause() instanceof EntityExistsException);
                // expected
            }

            em1.getTransaction().begin();
            em1.persist(new Person(1));
            try {
                em1.getTransaction().commit();
                fail("Should not be able to commit the same row a second time");
            } catch (RollbackException rbe) {
                assertTrue(rbe.getCause() instanceof EntityExistsException);
                // expected
            }
            em.close();
            em1.close();
        } finally {
            closeEMF(emf1);
        }
    }
View Full Code Here

            "openjpa.BrokerImpl", "SuppressBatchOLELogging=true", "openjpa.jdbc.DBDictionary", "batchLimit=-1",
            "openjpa.DataCache", "false");
    }

    public void test() throws Exception {
        OpenJPAEntityManagerSPI em1 = emf.createEntityManager();
        OpenJPAEntityManagerSPI em2 = emf.createEntityManager();

        em1.getTransaction().begin();
        List<EntityA> entities = new ArrayList<EntityA>();
        for (int i = 0; i < 25; i++) {
            EntityA a = createEntity();
            entities.add(a);
            em1.persist(a);
        }
        em1.getTransaction().commit();
        em1.clear();

        em1.getTransaction().begin();
        em2.getTransaction().begin();
        for (EntityA a : entities) {
            EntityA a1 = em1.find(EntityA.class, a.getId());
            EntityA a2 = em2.find(EntityA.class, a.getId());
            a1.setName("asdf");
            a2.setName("asdf2");
        }
        em1.getTransaction().commit();
        try {
            em2.getTransaction().commit();
        } catch (RollbackException e) {
            assertTrue(e.getMessage().contains("Suppressing"));
        }

    }
View Full Code Here

        super.setUp();
       
        // If using an Oracle DB, use sql92 syntax in order to get a correct
        // comparison of SQL.  This may not work on Oracle JDBC drivers
        // prior to 10.x
        OpenJPAEntityManagerSPI ojem = (OpenJPAEntityManagerSPI)em;
        dict = ((JDBCConfiguration) ojem.getConfiguration())
            .getDBDictionaryInstance();
        if (dict instanceof OracleDictionary) {
            dict.setJoinSyntax("sql92");
        }
    }
View Full Code Here

            fail("Didn't fail!");
        } catch (UserException re) {
            // expected

        }
        OpenJPAEntityManagerSPI em = emf.createEntityManager();
        em.getTransaction().begin();
        AllFieldTypes type = new AllFieldTypes();
        em.persist(type);
        em.getTransaction().commit();
        Object oid = em.getObjectId(type);
        assertEquals(Id.class, JPAFacadeHelper.toOpenJPAObjectId(cmd, oid).getClass());
    }
View Full Code Here

            (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
            createEntityManagerFactory("Access-EMFldDef",
            "org/apache/openjpa/persistence/access/" +
            "access-def-persistence.xml");
       
        OpenJPAEntityManagerSPI em = emf1.createEntityManager();
        verifyDefaultFieldAccess(em);
                       
        em.close();
        clear(emf1);
        closeEMF(emf1);
    }
View Full Code Here

            (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
            createEntityManagerFactory("Access-EMPropDef",
            "org/apache/openjpa/persistence/access/" +
            "access-def-persistence.xml");
       
        OpenJPAEntityManagerSPI em = emf1.createEntityManager();
        verifyDefaultPropertyAccess(em);

        em.close();
        clear(emf1);
        closeEMF(emf1);
    }
View Full Code Here

            (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
            createEntityManagerFactory("Access-PUFldDef",
            "org/apache/openjpa/persistence/access/" +
            "access-pudef-persistence.xml");
       
        OpenJPAEntityManagerSPI em = emf1.createEntityManager();
        verifyDefaultFieldAccess(em);
                       
        em.close();
        clear(emf1);
        closeEMF(emf1);
    }
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.