Search in sources :

Example 1 with JPAEntityManagerFactory

use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.

the class EntityGraphTest method testNamedGraphSpecification.

/**
 * Test of specification and registering of a named EntityGraph.
 */
public void testNamedGraphSpecification() {
    try {
        Set<String> graphNames = ((JPAEntityManagerFactory) emf).getEntityGraphNames();
        assertNotNull(graphNames);
        assertEquals("Number of EntityGraphs is wrong", 1, graphNames.size());
        JPAEntityGraph eg = (JPAEntityGraph) ((JPAEntityManagerFactory) emf).getNamedEntityGraph(graphNames.iterator().next());
        assertEquals("baseGraph", eg.getName());
        assertEquals(GraphBase.class, eg.getClassType());
        assertFalse(eg.getIncludeAllAttributes());
        List<AttributeNode<?>> egNodes = eg.getAttributeNodes();
        assertNotNull(egNodes);
        assertEquals(3, egNodes.size());
        boolean idPresent = false;
        boolean namePresent = false;
        boolean relationPresent = false;
        for (AttributeNode node : egNodes) {
            if (node.getAttributeName().equals("id")) {
                idPresent = true;
            } else if (node.getAttributeName().equals("name")) {
                namePresent = true;
            } else if (node.getAttributeName().equals("relation")) {
                relationPresent = true;
                Map<Class, Subgraph> subgraphsByClass = node.getSubgraphs();
                assertNotNull(subgraphsByClass);
                assertEquals(1, subgraphsByClass.size());
                Map.Entry<Class, Subgraph> subgraphEntry = subgraphsByClass.entrySet().iterator().next();
                assertEquals(GraphRelated.class, subgraphEntry.getKey());
                Subgraph subgraph = subgraphEntry.getValue();
                List<AttributeNode<?>> subNodes = subgraph.getAttributeNodes();
                assertNotNull(subNodes);
                assertEquals(1, subNodes.size());
                AttributeNode subNode = subNodes.iterator().next();
                assertEquals("id", subNode.getAttributeName());
            }
        }
        assertTrue("id not present", idPresent);
        assertTrue("name not present", namePresent);
        assertTrue("relation not present", relationPresent);
        // Should have been registered as dynamic FetchGroups
        PersistenceNucleusContext nucCtx = emf.unwrap(PersistenceNucleusContext.class);
        Set<FetchGroup> fgs = nucCtx.getFetchGroupManager().getFetchGroupsWithName("baseGraph");
        assertEquals(2, fgs.size());
    } finally {
    }
}
Also used : AttributeNode(javax.persistence.AttributeNode) JPAEntityGraph(org.datanucleus.api.jpa.JPAEntityGraph) JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) Subgraph(javax.persistence.Subgraph) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) FetchGroup(org.datanucleus.FetchGroup) Map(java.util.Map)

Example 2 with JPAEntityManagerFactory

use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testDatastoreIdColumnName.

public void testDatastoreIdColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(MyDatastoreId.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for datastore-id is incorrect", "mydatastoreid_id", factory.getColumnName(cmd1, ColumnType.DATASTOREID_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 3 with JPAEntityManagerFactory

use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testVersionColumnName.

public void testVersionColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(Trade4.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for version is incorrect", "version", factory.getColumnName(cmd1, ColumnType.VERSION_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 4 with JPAEntityManagerFactory

use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testTableName.

public void testTableName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(ByteArray.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.TABLE, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "bytearray", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.LOWER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"bytearray\"", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE);
    assertEquals("Table name is incorrect", "BYTEARRAY", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"BYTEARRAY\"", factory.getTableName(cmd1));
    AbstractClassMetaData cmd2 = mmgr.getMetaDataForClass(SimpleClass.class, clr);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "mysimpleclass", factory.getTableName(cmd2));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 5 with JPAEntityManagerFactory

use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.

the class EntityManagerFactoryTest method testSerialize.

public void testSerialize() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception on persist before serialisation", e);
            fail("Exception on persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Serialize the current EMF
        byte[] bytes = null;
        try {
            bytes = TestHelper.serializeObject(emf);
        } catch (RuntimeException re) {
            LOG.error("Error serializing EMF", re);
            fail("Error in serialization : " + re.getMessage());
        }
        // Deserialise the EMF
        EntityManagerFactory emf = null;
        try {
            emf = (JPAEntityManagerFactory) TestHelper.deserializeObject(bytes);
        } catch (RuntimeException re) {
            LOG.error("Error deserializing EMF", re);
            fail("Error in deserialization : " + re.getMessage());
        }
        JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
        assertNotNull(jpaEMF);
        assertNotNull(jpaEMF.getNucleusContext());
        assertNotNull(jpaEMF.getMetamodel());
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            TypedQuery<Person> q = em.createQuery("SELECT p FROM " + Person.class.getName() + " p", Person.class);
            List<Person> results = q.getResultList();
            assertEquals(1, results.size());
            Person p = results.get(0);
            assertEquals("Fred", p.getFirstName());
            assertEquals("Flintstone", p.getLastName());
            assertEquals("fred.flintstone@jpox.com", p.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception on retrieve after deserialisation", e);
            fail("Exception on retrieve after deserialisation : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) PersistenceException(javax.persistence.PersistenceException)

Aggregations

JPAEntityManagerFactory (org.datanucleus.api.jpa.JPAEntityManagerFactory)6 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 NucleusContext (org.datanucleus.NucleusContext)4 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)4 MetaDataManager (org.datanucleus.metadata.MetaDataManager)4 JPANamingFactory (org.datanucleus.store.schema.naming.JPANamingFactory)4 NamingFactory (org.datanucleus.store.schema.naming.NamingFactory)4 Map (java.util.Map)1 AttributeNode (javax.persistence.AttributeNode)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityTransaction (javax.persistence.EntityTransaction)1 PersistenceException (javax.persistence.PersistenceException)1 Subgraph (javax.persistence.Subgraph)1 FetchGroup (org.datanucleus.FetchGroup)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JPAEntityGraph (org.datanucleus.api.jpa.JPAEntityGraph)1 Person (org.datanucleus.samples.annotations.models.company.Person)1 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)1