Search in sources :

Example 6 with Manager

use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.

the class MetamodelTest method testInheritance.

/**
 * Test for the case with inheritance.
 */
public void testInheritance() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> mgrType = model.entity(Manager.class);
        assertNotNull(mgrType);
        assertEquals("Number of Manager attributes is wrong", 16, mgrType.getAttributes().size());
        assertEquals("Number of Manager singularAttributes is wrong", 13, mgrType.getSingularAttributes().size());
        assertEquals("Number of Manager pluralAttributes is wrong", 3, mgrType.getPluralAttributes().size());
        try {
            // Field in Manager
            Attribute attr = mgrType.getAttribute("subordinates");
            assertNotNull(attr);
            assertEquals(attr.getName(), "subordinates");
            assertEquals(attr.getJavaType(), Set.class);
            assertEquals(attr.getJavaMember().getName(), "subordinates");
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"subordinates\" field of " + Manager.class.getName());
        }
        try {
            // Field in Employee
            Attribute attr = mgrType.getAttribute("serialNo");
            assertNotNull(attr);
            assertEquals(attr.getName(), "serialNo");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "serialNo");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"serialNo\" field of " + Employee.class.getName());
        }
        try {
            // Primitive Field in Employee
            Attribute attr = mgrType.getAttribute("salary");
            assertNotNull(attr);
            assertEquals(attr.getName(), "salary");
            assertEquals(attr.getJavaType(), float.class);
            assertEquals(attr.getJavaMember().getName(), "salary");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertTrue(attr instanceof SingularAttribute);
            assertFalse(((SingularAttribute) attr).isOptional());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"salary\" field of " + Employee.class.getName());
        }
        try {
            // Field in Person
            Attribute attr = mgrType.getAttribute("firstName");
            assertNotNull(attr);
            assertEquals(attr.getName(), "firstName");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "firstName");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"firstName\" field of " + Person.class.getName());
        }
        // Supertype should not be null
        IdentifiableType empType = mgrType.getSupertype();
        assertNotNull(empType);
        IdentifiableType persType = empType.getSupertype();
        assertNotNull(persType);
        IdentifiableType superType = persType.getSupertype();
        assertNull(superType);
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Manager.class.getName());
    }
    try {
        EntityType<?> mugType = model.entity(ModeratedUserGroup.class);
        assertNotNull(mugType);
        assertNotNull(mugType.getId(long.class));
        assertEquals("id", mugType.getId(long.class).getName());
    } catch (IllegalArgumentException iae) {
        fail("Error in metamodel tests" + iae.getMessage());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) VersionedEmployee(org.datanucleus.samples.annotations.versioned.VersionedEmployee) Employee(org.datanucleus.samples.annotations.models.company.Employee) IdentifiableType(javax.persistence.metamodel.IdentifiableType) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Manager(org.datanucleus.samples.annotations.models.company.Manager) Metamodel(javax.persistence.metamodel.Metamodel) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 7 with Manager

use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.

the class MultithreadedTest method processFind.

protected String processFind(Object mgrId, boolean transaction) {
    EntityManager em = emf.createEntityManager();
    emf.getCache().evictAll();
    EntityTransaction tx = em.getTransaction();
    try {
        if (transaction) {
            tx.begin();
        }
        Manager mgr = em.find(Manager.class, mgrId);
        if (mgr == null) {
            return "Manager not found!";
        }
        if (!("The".equals(mgr.getFirstName()))) {
            return "Manager first name is wrong";
        }
        if (!("Boss".equals(mgr.getLastName()))) {
            return "Manager last name is wrong";
        }
        Set<Employee> emps = mgr.getSubordinates();
        if (emps == null) {
            return "Manager has null subordinates!";
        } else if (emps.size() != 100) {
            return "Manager has incorrect number of subordinates (" + emps.size() + ")";
        }
        if (transaction) {
            tx.commit();
        }
    } catch (Throwable thr) {
        LOG.error("Exception in find", thr);
        return "Exception in find : " + thr.getMessage();
    } finally {
        if (transaction && tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return null;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager)

Example 8 with Manager

use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.

the class MultithreadedTest method persistManagerWithEmployees.

/**
 * Convenience method to create a Manager with 100 Employees. Used by various tests in this file
 * @return The id of the manager object
 */
private Object persistManagerWithEmployees() {
    // Persist some data
    LOG.debug(">> Persisting data");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    Object mgrId = null;
    try {
        tx.begin();
        Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
        em.persist(mgr);
        for (int i = 0; i < 100; i++) {
            Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
            emp.setManager(mgr);
            mgr.addSubordinate(emp);
            em.persist(emp);
        }
        tx.commit();
        mgrId = mgr.getPK();
    } catch (Throwable thr) {
        LOG.error("Exception persisting objects", thr);
        fail("Exception persisting data : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    LOG.debug(">> Persisted data");
    // Verify the persistence
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        Query q = em.createQuery("SELECT e FROM " + Employee.class.getName() + " e");
        List<Employee> emps = q.getResultList();
        for (Employee e : emps) {
            LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
        }
        LOG.debug(">> Queried Employees");
        tx.commit();
    } catch (Throwable thr) {
        LOG.error("Exception checking objects", thr);
        fail("Exception checking data : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return mgrId;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Query(javax.persistence.Query) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager)

Example 9 with Manager

use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.

the class MultithreadedTest method processQueryAndDetachOneManyJoinBidir.

protected String processQueryAndDetachOneManyJoinBidir(boolean transaction) {
    List<Employee> results = null;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        if (transaction) {
            tx.begin();
        }
        Query q = em.createQuery("SELECT e FROM " + Employee.class.getName() + " e");
        List<Employee> emps = q.getResultList();
        for (Employee e : emps) {
            Manager mgr = e.getManager();
            if (e instanceof Manager) {
                // Get our subordinates loaded
                ((Manager) e).getSubordinates();
            } else {
                // Get subordinates of our Manager loaded
                mgr.getSubordinates();
            }
            e.getBestFriend();
        }
        results = new ArrayList<Employee>(emps);
        if (transaction) {
            tx.commit();
        }
    } catch (Throwable thr) {
        LOG.error("Exception query objects", thr);
        return "Exception in query : " + thr.getMessage();
    } finally {
        if (transaction && tx.isActive()) {
            tx.rollback();
        }
        // Detached the Employees and their loaded fields
        em.close();
    }
    for (Employee e : results) {
        try {
            LOG.debug(">> Employee: " + e.getFirstName() + " " + e.getLastName() + " bestFriend=" + e.getBestFriend());
            if (e instanceof Manager) {
                Set subs = ((Manager) e).getSubordinates();
                if (subs == null) {
                    return "Manager object didnt have its subordinates detached!";
                } else if (subs.size() != 100) {
                    return "Manager had " + subs.size() + " subordinates instead of 100";
                }
            } else {
                Manager mgr = e.getManager();
                if (mgr == null) {
                    return "Employee=" + e + " didnt have its manager set!";
                } else {
                    Set<Employee> subs = mgr.getSubordinates();
                    if (subs == null) {
                        return "Employee=" + e + " didnt have its subordinates set!";
                    } else if (subs.size() != 100) {
                        return "Employee=" + e + " has Manager with " + subs.size() + " subordinates instead of 100";
                    }
                    for (Employee subE : subs) {
                        subE.toString();
                    }
                }
            }
        } catch (Exception exc) {
            LOG.error(">> Exception thrown on check of results", exc);
            return "Exception checking results : " + exc.getMessage();
        }
    }
    return null;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Set(java.util.Set) Query(javax.persistence.Query) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager)

Aggregations

Manager (org.datanucleus.samples.annotations.models.company.Manager)9 EntityManager (javax.persistence.EntityManager)8 EntityTransaction (javax.persistence.EntityTransaction)8 Employee (org.datanucleus.samples.annotations.models.company.Employee)7 PersistenceException (javax.persistence.PersistenceException)5 StoreManager (org.datanucleus.store.StoreManager)5 NoResultException (javax.persistence.NoResultException)3 NonUniqueResultException (javax.persistence.NonUniqueResultException)3 Query (javax.persistence.Query)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Qualification (org.datanucleus.samples.annotations.models.company.Qualification)2 Set (java.util.Set)1 TypedQuery (javax.persistence.TypedQuery)1 Attribute (javax.persistence.metamodel.Attribute)1 IdentifiableType (javax.persistence.metamodel.IdentifiableType)1 ListAttribute (javax.persistence.metamodel.ListAttribute)1 MapAttribute (javax.persistence.metamodel.MapAttribute)1 Metamodel (javax.persistence.metamodel.Metamodel)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1