Search in sources :

Example 1 with GroupMember

use of org.jpox.samples.one_many.unidir_2.GroupMember in project tests by datanucleus.

the class RelationshipsTest method testOneToManyJoinTableIndexedList.

/**
 * Test of 1-N JoinTable indexed list (JPA2).
 */
public void testOneToManyJoinTableIndexedList() {
    EntityManagerFactory emf2 = getEMF(1, "JPATest", null);
    try {
        EntityManager em = emf2.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            UserGroup grp = new UserGroup(1, "JDO Expert Group");
            GroupMember member1 = new GroupMember(1, "Craig Russell");
            GroupMember member2 = new GroupMember(2, "David Jordan");
            List<GroupMember> members = new ArrayList<>();
            members.add(member1);
            members.add(member2);
            grp.setMembers(members);
            em.persist(grp);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = emf2.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + UserGroup.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            UserGroup grp = (UserGroup) result.get(0);
            Collection<GroupMember> members = grp.getMembers();
            assertEquals("Number of GroupMembers in UserGroup is incorrect", 2, members.size());
            Iterator<GroupMember> iter = members.iterator();
            boolean hasMem1 = false;
            boolean hasMem2 = false;
            GroupMember mem1 = iter.next();
            GroupMember mem2 = iter.next();
            if (mem1.getName().equals("Craig Russell")) {
                hasMem1 = true;
            }
            if (mem2.getName().equals("David Jordan")) {
                hasMem2 = true;
            }
            assertTrue("First member of user group is not present (in right place)", hasMem1);
            assertTrue("Second member of user group is not present (in right place)", hasMem2);
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(emf2, UserGroup.class);
        clean(emf2, GroupMember.class);
    }
    emf2.close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 2 with GroupMember

use of org.jpox.samples.one_many.unidir_2.GroupMember in project tests by datanucleus.

the class JPQLQueryTest method testJoinMultipleFields.

/**
 * Test for join across multiple fields in single join.
 */
public void testJoinMultipleFields() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            UserGroup grp = new UserGroup(101, "JPOX Users");
            GroupMember member1 = new GroupMember(201, "Joe User");
            MemberDetails memDets = new MemberDetails(301, "Joe", "User");
            member1.setDetails(memDets);
            grp.getMembers().add(member1);
            em.persist(grp);
            em.flush();
            List result = em.createQuery("SELECT G FROM " + UserGroup.class.getName() + " G INNER JOIN G.members.details D").getResultList();
            assertEquals(1, result.size());
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(UserGroup.class);
        clean(GroupMember.class);
        clean(MemberDetails.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) ExpertGroupMember(org.datanucleus.samples.annotations.one_many.unidir_2.ExpertGroupMember) GroupMember(org.datanucleus.samples.annotations.one_many.unidir_2.GroupMember) EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) MemberDetails(org.datanucleus.samples.annotations.one_many.unidir_2.MemberDetails) ModeratedUserGroup(org.datanucleus.samples.annotations.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.datanucleus.samples.annotations.one_many.unidir_2.UserGroup)

Example 3 with GroupMember

use of org.jpox.samples.one_many.unidir_2.GroupMember in project tests by datanucleus.

the class JPQLQueryTest method testInnerJoinOneToManyUniFKOwner.

/**
 * Test for Inner Join 1-N unidirectional FK relation from the owner side.
 */
public void testInnerJoinOneToManyUniFKOwner() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            UserGroup grp = new UserGroup(101, "JPOX Users");
            GroupMember member1 = new GroupMember(201, "Joe User");
            grp.getMembers().add(member1);
            em.persist(grp);
            em.flush();
            List result = em.createQuery("SELECT G FROM " + UserGroup.class.getName() + " G INNER JOIN G.members M").getResultList();
            assertEquals(1, result.size());
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(UserGroup.class);
        clean(GroupMember.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) ExpertGroupMember(org.datanucleus.samples.annotations.one_many.unidir_2.ExpertGroupMember) GroupMember(org.datanucleus.samples.annotations.one_many.unidir_2.GroupMember) EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) ModeratedUserGroup(org.datanucleus.samples.annotations.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.datanucleus.samples.annotations.one_many.unidir_2.UserGroup)

Example 4 with GroupMember

use of org.jpox.samples.one_many.unidir_2.GroupMember in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateApplicationIdentityWith1toN.

public void testReplicateApplicationIdentityWith1toN() {
    PersistenceManagerFactory pmf1 = null;
    PersistenceManagerFactory pmf2 = null;
    try {
        // Connect to "datastore1"
        Properties props = new Properties();
        props.setProperty(PropertyNames.PROPERTY_ATTACH_SAME_DATASTORE, "false");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM, "Classes");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_CLASSNAMES, "org.jpox.samples.one_many.unidir_2.GroupMember,org.jpox.samples.one_many.unidir_2.ExpertGroupMember," + "org.jpox.samples.one_many.unidir_2.UserGroup,org.jpox.samples.one_many.unidir_2.ModeratedUserGroup");
        pmf1 = getPMF(1, props);
        // Persist data to "datastore1"
        PersistenceManager pm1 = pmf1.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        Object holderId = null;
        try {
            tx1.begin();
            ModeratedUserGroup holder = new ModeratedUserGroup(1, "HolderA", "First A");
            List<GroupMember> elements = new ArrayList<GroupMember>();
            elements.add(new ExpertGroupMember(25, "ElementB1", "First B"));
            elements.add(new ExpertGroupMember(26, "ElementB2", "Second B"));
            elements.add(new GroupMember(27, "ElementB3"));
            elements.add(new GroupMember(28, "ElementB4"));
            holder.setMembers(elements);
            pm1.makePersistent(holder);
            tx1.commit();
            holderId = JDOHelper.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception thrown creating data in datastore 1", e);
            e.printStackTrace();
            return;
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach holder from "datastore1"
        UserGroup detachedHolder = null;
        pm1 = pmf1.getPersistenceManager();
        tx1 = pm1.currentTransaction();
        try {
            pm1.getFetchPlan().setGroups(new String[] { FetchPlan.DEFAULT, FetchPlan.ALL });
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            tx1.begin();
            ModeratedUserGroup holder = (ModeratedUserGroup) pm1.getObjectById(holderId);
            detachedHolder = (ModeratedUserGroup) pm1.detachCopy(holder);
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown detaching data from datastore 1", e);
            fail("Exception in detach with datastore 1 : " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Connect to "datastore2"
        pmf2 = getPMF(2, props);
        // Attach data to "datastore2"
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            pm2.makePersistent(detachedHolder);
            tx2.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown attaching data to datastore 2", e);
            fail("Exception in attach with datastore 2 : " + e.getMessage());
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } catch (Exception e) {
        fail("Exception on attach to datastore 2 : " + e.getMessage());
    } finally {
        // Clean out our data
        clean(ModeratedUserGroup.class);
        clean(UserGroup.class);
        clean(ExpertGroupMember.class);
        clean(GroupMember.class);
    }
}
Also used : ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) ArrayList(java.util.ArrayList) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 5 with GroupMember

use of org.jpox.samples.one_many.unidir_2.GroupMember in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterCollectionContains.

/**
 * Test of filter with collectionField.contains(element).
 */
public void testFilterCollectionContains() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<UserGroup> instances = new ArrayList<>();
        UserGroup grp1 = new UserGroup(101, "First Group");
        UserGroup grp2 = new UserGroup(102, "Second Group");
        UserGroup grp3 = new UserGroup(103, "Third Group");
        GroupMember mem1 = new GroupMember(201, "Donald Duck");
        GroupMember mem2 = new GroupMember(202, "Mickey Mouse");
        GroupMember mem3 = new GroupMember(203, "Minnie Mouse");
        grp1.getMembers().add(mem1);
        grp2.getMembers().add(mem1);
        grp2.getMembers().add(mem2);
        grp3.getMembers().add(mem2);
        grp3.getMembers().add(mem3);
        instances.add(grp1);
        instances.add(grp2);
        instances.add(grp3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(UserGroup.class, "members.contains(el) && el.name == 'Minnie Mouse'");
        q.declareVariables(GroupMember.class.getName() + " el");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        UserGroup grp = (UserGroup) results.get(0);
        assertEquals("Result instance has wrong name", "Third Group", grp.getName());
        tx.commit();
    } catch (Exception e) {
        LOG.info(">> Unexpected exception thrown during test", e);
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Aggregations

ArrayList (java.util.ArrayList)5 List (java.util.List)4 EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 GroupMember (org.jpox.samples.one_many.unidir_2.GroupMember)3 UserGroup (org.jpox.samples.one_many.unidir_2.UserGroup)3 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 ExpertGroupMember (org.datanucleus.samples.annotations.one_many.unidir_2.ExpertGroupMember)2 GroupMember (org.datanucleus.samples.annotations.one_many.unidir_2.GroupMember)2 ModeratedUserGroup (org.datanucleus.samples.annotations.one_many.unidir_2.ModeratedUserGroup)2 UserGroup (org.datanucleus.samples.annotations.one_many.unidir_2.UserGroup)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 JDOException (javax.jdo.JDOException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 JDOQuery (org.datanucleus.api.jdo.JDOQuery)1