Search in sources :

Example 1 with Department

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

the class EmbeddedTest method testOneToManyWithEmbeddedId.

/**
 * Test of 1-N relation when the owner has an embedded id.
 */
public void testOneToManyWithEmbeddedId() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Department dept = new Department("Marketing");
            DepartmentPK deptPK = new DepartmentPK(101, "Mkt");
            dept.setPrimaryKey(deptPK);
            Project prj1 = new Project("DN 2.0", 100000);
            dept.getProjects().add(prj1);
            em.persist(dept);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown creating data", e);
            fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Query q = em.createQuery("SELECT d FROM " + Department.class.getName() + " d");
            List<Department> depts = q.getResultList();
            assertNotNull("Returned Department List is null", depts);
            assertEquals("Number of Departments is incorrect", 1, depts.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception thrown retrieving data", e);
            fail("Exception thrown while retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Department.class);
        clean(Project.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Project(org.datanucleus.samples.annotations.models.company.Project) EntityManager(javax.persistence.EntityManager) Department(org.datanucleus.samples.annotations.models.company.Department) Query(javax.persistence.Query) DepartmentPK(org.datanucleus.samples.annotations.models.company.DepartmentPK)

Example 2 with Department

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

the class EmbeddedTest method testPersistenceAndDeletionWithEmbeddedId.

/**
 * Test for basic persistence and deletion with a class using @EmbeddedId.
 */
public void testPersistenceAndDeletionWithEmbeddedId() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            // Persist the object and query it
            tx.begin();
            Department d = new Department("Bureaucracy");
            DepartmentPK pk = new DepartmentPK(Integer.valueOf(1), "1");
            d.setPrimaryKey(pk);
            em.persist(d);
            List result = em.createQuery("SELECT T FROM " + Department.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            assertEquals(pk.getIdString(), ((Department) result.get(0)).getPrimaryKey().getIdString());
            tx.commit();
            // Retrieve the object and delete it
            tx.begin();
            result = em.createQuery("SELECT T FROM " + Department.class.getName() + " T").getResultList();
            d = (Department) result.get(0);
            em.remove(d);
            em.flush();
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist and query", e);
            fail("Exception during persist + query : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Department.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Department(org.datanucleus.samples.annotations.models.company.Department) List(java.util.List) DepartmentPK(org.datanucleus.samples.annotations.models.company.DepartmentPK)

Aggregations

EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 Department (org.datanucleus.samples.annotations.models.company.Department)2 DepartmentPK (org.datanucleus.samples.annotations.models.company.DepartmentPK)2 List (java.util.List)1 Query (javax.persistence.Query)1 Project (org.datanucleus.samples.annotations.models.company.Project)1